Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write to s3 via a stream using s3 java sdk

Normally when a file has to be uploaded to s3, it has to first be written to disk, before using something like the TransferManager api to upload to the cloud. This cause data loss if the upload does not finish on time(application goes down and restarts on a different server, etc). So I was wondering if it's possible to write to a stream directly across the network with the required cloud location as the sink.

like image 790
Aditya Vivek Avatar asked Jul 09 '18 12:07

Aditya Vivek


People also ask

How do I move files from one S3 bucket to another in Java?

You can copy an object from one bucket to another by using the AmazonS3 client's copyObject method. It takes the name of the bucket to copy from, the object to copy, and the destination bucket name. s3. copyObject(from_bucket, object_key, to_bucket, object_key); } catch (AmazonServiceException e) { System.

How does uploading to S3 work?

When you upload a folder, Amazon S3 uploads all of the files and subfolders from the specified folder to your bucket. It then assigns an object key name that is a combination of the uploaded file name and the folder name. For example, if you upload a folder named /images that contains two files, sample1.


2 Answers

It is possible:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
    .build();
s3Client.putObject("bucket", "key", youtINputStream, s3MetData)

AmazonS3.putObject

like image 160
Cherry Avatar answered Oct 11 '22 12:10

Cherry


You don't say what language you're using, but I'll assume Java based on your capitalization. In which case the answer is yes: TransferManager has an upload() method that takes a PutObjectRequest, and you can construct that object around a stream.

However, there are two important caveats. The first is in the documentation for PutObjectRequest:

When uploading directly from an input stream, content length must be specified before data can be uploaded to Amazon S3

So you have to know how much data you're uploading before you start. If you're receiving an upload from the web and have a Content-Length header, then you can get the size from it. If you're just reading a stream of data that's arbitrarily long, then you have to write it to a file first (or the SDK will).

The second caveat is that this really doesn't prevent data loss: your program can still crash in the middle of reading data. One thing that it will prevent is returning a success code to the user before storing the data in S3, but you could do that anyway with a file.

like image 29
guest Avatar answered Oct 11 '22 11:10

guest