- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import com.amazonaws.auth.profile.ProfileCredentialsProvider;
- import com.amazonaws.services.s3.AmazonS3;
- import com.amazonaws.services.s3.AmazonS3Client;
- import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
- import com.amazonaws.services.s3.model.CannedAccessControlList;
- import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
- import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
- import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
- import com.amazonaws.services.s3.model.PartETag;
- import com.amazonaws.services.s3.model.UploadPartRequest;
- public class MultipartPublicFileUpload {
- static AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
- static public boolean multiPartFileUploadWithPublicAccess(String existingBucketName, String keyName, String filePath) {
- // Create a list of UploadPartResponse objects. You get one of these
- // for each part upload.
- List<PartETag> partETags = new ArrayList<PartETag>();
- // Step 1: Initialize.
- InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(existingBucketName, keyName);
- initRequest.setCannedACL(CannedAccessControlList.PublicRead);//This is where we are enabling this file for public access
- InitiateMultipartUploadResult initResponse
- = s3Client.initiateMultipartUpload(initRequest);
- File file = new File(filePath);
- long contentLength = file.length();
- long partSize = 5242880; // Set part size to 5 MB.
- try {
- // Step 2: Upload parts.
- long filePosition = 0;
- for (int i = 1; filePosition < contentLength; i++) {
- // Last part can be less than 5 MB. Adjust part size.
- partSize = Math.min(partSize, (contentLength - filePosition));
- // Create request to upload a part.
- UploadPartRequest uploadRequest = new UploadPartRequest()
- .withBucketName(existingBucketName).withKey(keyName)
- .withUploadId(initResponse.getUploadId()).withPartNumber(i)
- .withFileOffset(filePosition)
- .withFile(file)
- .withPartSize(partSize);
- // Upload part and add response to our list.
- partETags.add(
- s3Client.uploadPart(uploadRequest).getPartETag());
- filePosition += partSize;
- }
- // Step 3: Complete.
- CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(
- existingBucketName,
- keyName,
- initResponse.getUploadId(),
- partETags);
- s3Client.completeMultipartUpload(compRequest);
- return true;
- } catch (Exception e) {
- s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(
- existingBucketName, keyName, initResponse.getUploadId()));
- return false;
- }
- }
- public static void main(String[] args) throws IOException {
- String existingBucketName = "myphotowall";
- String keyName = "photo/myuploadedtestfile3.txt";
- String filePath = "/tmp/mytestfile.txt";
- multiPartFileUploadWithPublicAccess(existingBucketName, keyName, filePath);
- }
- }
Please refer to http://mohiplanet.blogspot.com/2014/12/amazon-web-services-s3-api-java-client.html for updating keys with correct content type or setting correct content type with object metadata while uploading.
References :
http://docs.aws.amazon.com/AmazonS3/latest/dev/llJavaUploadFile.html
No comments:
Post a Comment