Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a txt to Amazon S3

I am using the s3 sdk to upload a string (which will be change into a txt file). It is ok using the sdk. But since the sdk is only available for new browser (eg: ie10+) I need to upload my file with another way (for old browser)

For image file I use an input (type file) and a form for the upload

<form id="urlform" enctype="multipart/form-data" method="post" target="upload_target" class="inline">
    <input type="hidden" name="key" value="{{$parent.keyurl}}">
    <input type="hidden" name="acl" value="public-read">
    <input type="hidden" name="AWSAccessKeyId" value="{{$parent.awSAccessKeyIdUrl}}">
    <input type="hidden" name="success_action_redirect" value="{{$parent.redirectionUrl}}">
    <input type="hidden" name="x-amz-meta-filename" value="{{$parent.filenameurl}}">
    <input type="hidden" name="policy" value="{{$parent.policyurl}}">
    <input type="hidden" name="signature" value="{{$parent.signatureurl}}">
    <input type="hidden" name="x-amz-security-token" value="{{$parent.urlSessionToken}}">
    <div>
        <label>
        </label>
        <input type="file" name="file" id="urlfileinput">
    </div>
</form> 

this solution only works with input type file .

For security reason I can't change the value of the input with jquery.

Is there another way to upload text (using the rest api perhaps) ?

like image 211
BironDavid Avatar asked Apr 25 '14 08:04

BironDavid


People also ask

Can we upload txt file in S3 bucket?

One of the most common ways to upload files on your local machine to S3 is using the client class for S3. You need to provide the bucket name, file which you want to upload and object name in S3. When you run this function, it will upload “sample_file. txt” to S3 and it will have the name “sample1.

How do I upload data to Amazon S3?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.


1 Answers

I'm curious as to why you're using a URL form to submit this to Amazon... This is hugely insecure since you're giving out your AWS Access Key to everyone. First, you need to look into using the Amazon SDK for Javascript, which should work easy enough with Angular as a dependency.

Next, you need to look into doing CORS on your S3 Bucket (cross domain resource sharing) so that you can in fact 'upload' something on S3 from anywhere without the need for authentication (be careful with this since everyone will have access to it and can upload anything, and if you don't configure it properly, can give access to other things like delete).

Lastly, you simply need to use the SDK's AWS.S3().putObject() function to upload whatever you need to your public S3 bucket.

like image 84
J_A_X Avatar answered Sep 17 '22 17:09

J_A_X