Upload a file directly to a folder in bucket using android SDK

4.1k views Asked by At

I am using following code to upload a file into the S3 bucket:

//upload code start
AWSCredentials credential = new BasicAWSCredentials(AppConstants.AWS_ACCESS_KEY_ID, AppConstants.AWS_SECRET_ACCESS_ID);
TransferManager manager = new TransferManager(credential);
// Transfer a file to an S3 bucket.
Upload upload = manager.upload("bucket_name", AppConstants.AWS_ACCESS_KEY_ID, file);
while (!upload.isDone()) {
    Thread.sleep(200);
}
//upload code end

I want to put this into a directory named "android_uploads" inside my bucket but Transfer Manager is uploading all the files into root of the bucket.

Any idea how i can achieve this?

3

There are 3 answers

0
Wade Matveyenko On BEST ANSWER

Your call to upload() is incorrect. It should be like:

Upload upload = manager.upload("bucket_name", "android_uploads/" + fileName, file);
0
Rajan Bhavsar On

You should able to Upload any file type to Amazon s3 by simple code as follows:

PutObjectRequest por = new PutObjectRequest(
                                BUCKET_NAME, Key,
                                file);

por.setCannedAcl(CannedAccessControlList.PublicReadWrite);
                        s3Client.putObject(por);

i was tried this code for both Image and Text File Uploadation.

0
Srikanth Shanigaram On
Upload upload = manager.upload("bucket_name","filePath where to be stored in bucket sub folder or directly to the bucket", file);