Update zip file in S3 using Java - java

I have a java application in which I would like to process around 10GB records of file and zip them to a single folder and upload to S3. Since the overall size is around 10GB I cannot add all the files in memory and then upload to S3, and hence I would need to create a zip file in S3 and update the contents of the zip file by partitioning my files. Is there any means by which I can update an existing zip file in S3 without downloading to my local folder?

You can use aws java sdk for it
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.398</version>
</dependency>
Create a amazon s3 client using following
BasicAWSCredentials credentials = new BasicAWSCredentials("access_key", "secret_key");
AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
Create a TransferManager and set the MultipartUploadThresholdoad.
Amazon S3 impose minimum part size of 5mb, So we are using 5mb here. You can increase the size as per your requirement.
TransferManager tm = TransferManagerBuilder.standard()
.withS3Client(amazonS3)
.withMultipartUploadThreshold((long) (5 * 1024 * 1024))
.build();
Set yours S3 bucket name where you want to upload and keyName will used to name the uploaded file. tm.upload will start the uploading process in background.
String bucketName = "my-bucket";
String keyName = "mydata.zip";
File file = new File("path_to_file/mydata.zip");
Upload upload = tm.upload(bucketName, keyName, file);
waitForCompletion is the blocking call and will return result once function completes its execution to upload file to s3.
try {
upload.waitForCompletion();
} catch (AmazonClientException e) {
// ...
}

Related

Need to check the object integrity of a multipart file uploaded through TransferManager SHA256 aws sdk java

There is a sample code provided by the AWS API tp upload the file using the TransferManager
.withRegion(clientRegion)
.withCredentials(new ProfileCredentialsProvider())
.build();
TransferManager tm = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.build();
// TransferManager processes all transfers asynchronously,
// so this call returns immediately.
Upload upload = tm.upload(bucketName, keyName, new File(filePath));
System.out.println("Object upload started");
// Optionally, wait for the upload to finish before continuing.
upload.waitForCompletion();
System.out.println("Object upload complete");
I need help in checking the integrity of the file before uploading in with the SHA-256 Digest.

storing pdf files on amazon s3 using itext

This is my first time using amazon s3 and I want to store pdf files that I create using itext in java spring.
The code (hosted on ec2 instance) creates a pdf that I would like to store somewhere. I am exploring if amazon s3 can hold those files. Eventually I would like to retrieve it as well. Can this be done using itext and java spring? Any examples would be great.
To Upload Files to Amazon s3 You need to use putObject method of AmazonS3Client class like this:
AWSCredentials credentials = new BasicAWSCredentials(appId,appSecret);
AmazonS3 s3Client = new AmazonS3Client(credentials);
String bucketPath = "YOUR_BUCKET_NAME/FOLDER_INSIDE_BUCKET";
InputStream is = new FileInputStream("YOUR_PDF_FILE_PATH");
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(is.available());
s3Client.putObject(new PutObjectRequest(bucketPath,"YOUR_FILE.pdf", is, meta).withCannedAcl(CannedAccessControlList.Private));
And to get file from S3, You need to generate a pre-signed URL to access private file from S3 or if your files are public then you can directly access your file by hitting link of file in your browser, The link for your file will be available in AWS S3 console.
Also we have specified CannedAccessControlList.Private in the above upload code which means we are making permission of file as private So we need to generate presigned URL to access file like this:
AWSCredentials credentials = new BasicAWSCredentials(appId,appSecret);
AmazonS3 s3Client = new AmazonS3Client(credentials);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest("YOUR_BUCKET_NAME", "FOLDER_INSIDE_BUCKET/YOUR_FILE.pdf");
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
Date expiration = new Date();
long milliSeconds = expiration.getTime();
milliSeconds += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(milliSeconds);
generatePresignedUrlRequest.setExpiration(expiration);
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
String finalUrl = url.toString();

Android amazon s3 upload part size

I'm sending files to amazon s3 server like this and really need to change part sizes of sending file from default amazon (5mb) to 1 mb, is there any way to do that?
TransferObserver observer = transferUtility.upload(
"mydir/test_dir", /* The bucket to upload to */
data.getData().getLastPathSegment(), /* The key for the uploaded object */
root /* The file where the data to upload exists */
);;
The minimum part size for S3 multipart uploads is 5MB. (See http://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadUploadPart.html). The Transfer Utility uses the smallest allowable part size, which is usually 5MB.

Server side upload to Blobstore - Java Google Appengine

I create data in the server (gae) and I want to store it in Blobstore. I saw many answers on how to do this giving a BlobStore URL to the client, but there is no client or HTTP request: it's just an asynchronous task.
Then I guess I should use createUploadUrl(), and instead of giving this URL to a client, from my code HTTP Post my data to it via URL Fetch. This looks weird, isn't there another API for this?
Let's say that the files I want in Blobstore are already stored in my GCS default bucket. Can I just tell Blobstore about them using the gcs location "/gs/bucketname/file"? I tried this by
GcsFilename filename = new GcsFilename(bucketName, fileId);
String gcsKey = "/gs/" + bucketName + "/" + filename.getObjectName();
BlobKey blobKey = blobstoreService.createGsBlobKey(gcsKey);
GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, GcsFileOptions.getDefaultInstance());
ObjectOutputStream oout = new ObjectOutputStream(Channels.newOutputStream(outputChannel));
oout.writeObject(myDataObjectToPersist);
oout.close();
// ...at some other point I have checked the file is correctly stored in
// GCS and I can fetch it using /gs/bucket/fileId
// but it doesn't seem to be in Blobstore, so when
InputStream stream = new BlobstoreInputStream(new BlobKey(blobKey.keyString))
// ... this gives a BlobstoreInputStream$BlobstoreIOException: BlobstoreInputStream received an invalid blob key...
Is this something conceptually wrong - like if I use GcsOutputChannel to save it I will not get it from Blobstore even if I create a BlobKey, or is it something that could work but I just did something wrong?
1K thanks
Why would you want to store the file in blobstore as opposed to writing and reading it directly from GCS?
Yes, you can create a BlobKey for a file stored in GCS, and can use the key in some of the blobstore API (such as fetchData and serve) but unfortunately not in all.
Some of the blobstore API (such as BlobstoreInputStream) depends on BlobInfo and that is not created when using the GCS client.

AWS S3 file upload integration with dropwizard?

I am new to Dropwizard. I want to implement AWS S3 File upload service in my project.
I am not getting any tutorial to upload file on AWS S3 through dropwizard.
I have added following dependecies in pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.9.28.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.17</version>
</dependency>
I have registered MultiPartfeature.class in Application class's run() method as -
environment.jersey().register(MultiPartFeature.class);
Then in resource class defined method as -
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("/updateProfilePicture")
public String updateProfile(#FormDataParam("file") InputStream fileInputStream,
#FormDataParam("file") FormDataContentDisposition contentDispositionHeader) throws Exception {
String url = "";
AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials("MY-ACCESS-KEY", "MY-SECRET_KEY"));
try {
File file = new File(contentDispositionHeader.getFileName());
PutObjectResult putObjectResult = s3client.putObject(new PutObjectRequest("BUCKET-NAME", s3SourceFactory.getSecretAccessKey(), fileInputStream, new ObjectMetadata()));
} catch (AmazonServiceException ase) {
ase.printStackTrace();
} catch (AmazonClientException ace) {
ace.printStackTrace();
}
return url;
}
But at run-time it shows the following log -
com.amazonaws.services.s3.AmazonS3Client: No content length specified for stream data. Stream contents will be buffered in memory and could result in out of memory errors.
How can I get the url of uploaded file? How to check file is uploaded through coding? Am I missing anything? Does anybody have any idea about this. If there any tutorial available with dropwizard, it will be helpful.
Thanks in advance
If the access key and secret key are correct. My guess is towards the S3 bucket permissions, once you go to your s3 bucket on aws console, to the right top you will find "properties" , once you open that you will have permissions make sure you gave an entry for your server there.

Categories

Resources