Simple WebApp - Upload file to S3 via HTML / AWS Javascript SDK - javascript

Im just starting down my dev journey and need some advice on how to approach a simple app I'm working on. I do not have a good understanding of
modern web development.
What I am looking to achieve is to upload a video or image via a browser form / html form to Amazon S3.
Ideally, I want to leverage the AWS node.js SDK but keep my front end as basic as possible (i.e bootstrap page + html changes) I acknowledge that I could do a straight http operation but would still like to leverage the SDK for now.
I have my html,form and css created (using bootstrap), but do not understand how to connect the form to a node.js script that does the authorization/PUT?
Can I even go from the form, and pass the file to the script to be uploaded?
Thanks for any advice!!! :D

Check out the AWS documentation. They even have an example for your use case: Uploading Photos to Amazon S3 from a Browser

You can use, AWS CloudFront Signed URLs to perform the upload. The flow involved are as follows.
From the browser, you will request for a url, with an expiration time, to allow upload a file to a bucket. You can write Sign url creation using NodeJS Backend and AWS SDK as given in this example, after authenticating the user.
Using the Signed URL and AWS JavaScript SDK for S3, browser can directly upload the file to S3.
For more information about Signed URLs, check How Signed URLs Work.

Related

How to download Image from browser & upload to Amazon S3

How can I allow the client to upload an image in the browser, and then I upload it to Amazon S3? I have been looking around a lot and have found no resources explaining how to do this.
Are there any tutorials that I could follow?
Are there any libraries that I should use for this?
I am using AngularJS on the frontend and Node.js on the backend.
In short, look for two different tutorials. One for uploading from a client to a server, one for uploading from a server to S3.
StackOverflow discourages linking to specific tutorials, but there are lots of them out there, so it shouldn't be too tricky to track down.
For the client-to-server, you'll want to do a basic HTML form upload up to the server, then snag the data. You can temporarily write it to your file system (if you're on Linux, the /tmp directory is a good place to stash it).
After that, just upload from your server to S3. Amazon itself has some good documentation on that. The s3 package for Node also has good examples: https://www.npmjs.com/package/s3
It's also possible to go straight from the browser to S3, which may be better depending on your use case. Check this out: http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html
You're going to need the AWS SDK for node. Then they have a pretty comprehensive developper guide. You may have to read up on credential management, too.
the procedure would be as follows
user uploads the image from browser to your server (I'd recomend plain form upload, unless you feel ok with uploading via ajax)
then your server uses the SDK to save to S3
you display back info to the user (link to the image, upload status ?).
you could also use pre-signed posts but that seems more advanced and I haven't seen info on it for Node.

Upload files from browser to S3

I have a pre signed url that allows you to PUT documents objects into S3.
http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjectPreSignedURLDotNetSDK.html
How do I put a file from the browser to S3 using javascript? I am using angular, but I am open to using any javascript library.
I believe I could POST to my server, and then PUT the object on the amazon server, but I would prefer to do it from the browser.
I have changed the CORS settings on S3 to allow PUTs.
I was planning to use angular file upload, but it is hard coded to POST not PUT.
https://github.com/danialfarid/angular-file-upload
Amazon has a guide (here) that describes how to POST-upload a file into your S3 bucket. It relies on an input <form> signed with your AWS private key. You can specify restrictions on the target directory, as well as file-size restrictions.
It's a bit annoying to use, because you have to duplicate most of the fields in both the <form> and the signed policy, but it seems to work.
After the POST, S3 will redirect the browser to a URL you specify in the form (with parameters specifying the name of the uploaded file, etc.). This isn't ideal for Angular web sites, which tend to be "applications" rather than a set of discrete pages, but you could probably work with it.
On my Angular site, I did the POST in Javascript using $http.post() and passed all the appropriate form data. Unfortunately, I was always getting "cancelled" errors, even though the uploads were actually successful. In my case, I just double-checked by downloading the file with $http.get() and comparing it to the original data... but this was only a viable solution because my files were only a couple of KB.

How to upload a file to amazon S3 without passing it by a server?

I'm looking for a front-end solution for uploading files to amazon s3 (that is, not passing them through my server.
The solution I have found is
https://code.google.com/p/swfupload/
It might do the job, but it requier flash and this is the first sentence of the project description is:
SWFUpload has not been under active development for several years.
Here are my desired features, though none of them are nessesary
No plugins
Built with Amazon S3 in mind, handling buckets authentification etc.
Some way to see file upload-progress
Support from IE8+
kgu87 is correct, this article pretty much explains the entire process to upload files directly to S3 without passing them trough your own server.
You can also check out the AWS docs related to this on:
http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingHTTPPOST.html
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
If you're looking for an upload tool that supports HTML5 uploads directly to S3, check out Plupload
They have a great article that explains how to set it up:
https://github.com/moxiecode/plupload/wiki/Upload-to-Amazon-S3
The documentation describes a PHP service that's used to generate a policy and signature (both are required for S3 to accept your download) but you can use any language to generate those. Also, in certain use cases, you can just generate a one-time policy with a very high expiration time and hard code it into your upload form.
You could start by using this tutorial as a baseline , if you are asking about uploading from your web app -
http://aws.amazon.com/articles/1434
You could use this tool: http://aws.amazon.com/customerapps/Amazon-S3/Consumers/2069 free, and works in most browsers; doesn't require a server.
S3 Browser is a free client interface for users of the Amazon S3 service.
It provides an easy to use GUI to manage your storage buckets and allows you to:
- Browse, create, delete Amazon S3 buckets
- Upload and download files to and from Amazon S3
- Create public URLs to share the files.
- Keep your files backed up on a multiple data centers.
- Set Access Control on buckets and files.
S3 Browser is free for non-commercial use.

Upload files from mobile on server using jquery

I have a mobile application which needs the user to attach some files from his mobile and upload it on the server.
Is there a way to upload files on the server from mobile using JQUERY/JAVASCRIPT ?
I found many doin it with PHP/ASP.NET-c#...but my need is HTML with JAVASCRIPT/JQUERY...!!
FURTHER INFO ON MY APP (Kindly neglect it if not needed):
I have my front-end in HTML/CSS..I have a WCF service running on my server that does the communication with the database..I use the WCF service by making ajax calls from my client side..
I achieved all the other operations such as insert/delete/update/retrieve data from DB using these services only..
I was wondering if there is any possibility to do the "upload files on the server" task also using these webservices...but didnt get any idea about it !!!
Here is another question exactly precise to my need !! File upload service using WCF along with jquery(AJAX) and html client
Sorry if i m wrong anywhere..This stuff is totally new to me and so i need all your guidance..
To save an uploaded file, you need an server side script (PHP, ASP e.g.) to move the uploaded file from the servers tmp directory, to your desired location.
Maybe can you edit your serves configuration so it writes the uploaded files automatic to your desired location, but I wouldn't suggest that due to security reasons.
Is this a native application? If it's just a website then you'll struggle as safari on iPhone doesn't support file uploads. If it's a phonegap application you could maybe hook into the file API...
Ok some of the points worth mentioning.
When you wanted to upload file to server you have to POST file with multipart content type of the form. For this you might need to write WCF service on server end accordingly. I am not having good experience of WCF but this post should help may be
<form method="POST" enctype="multipart/form-data" >
HTML5 file API and XHR Level 2 specification you can upload files to server. As they are new features and JS and DOM involved yes you can consum and do it in JQuery as well as long as your browser is supporting them.
With these things i am not sure if there should be any issue while uploading files to the server.

Access files on Amazon S3 using HTML5 / JavaScript

Is it possible to access the files on Amazon S3 using JavaScript / HTML5. I basically plan to access these files in a PhoneGap app.
To be more specific, I mean without any server in middle i.e. making direct calls to Amazon S3. Is it possible?
Yes. You can enable CORS for your bucket so that objects can be loaded via XMLHttpRequest.
http://aws.typepad.com/aws/2012/08/amazon-s3-cross-origin-resource-sharing.html
http://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html
When using PhoneGap/Cordova specifically, you may be able to bypass enabling CORS on the server by whitelisting the domain from within your app:
http://docs.phonegap.com/en/2.3.0/guide_whitelist_index.md.html
Answering my own question - I came up with a JavaScript S3 request signer using which I was able to sign the URL and access the files on S3 from JavaScript.
Anybody needs any details, please leave comment and would be happy to assist.
Cheers

Categories

Resources