Friday, 22 December 2017

How to create and delete folder and files from AWS S3 bucket in Java

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectSummary;
public class AmazonS3Example {

private static final String SUFFIX = "/";

public static void main(String[] args) {
// credentials object identifying user for authentication
// user must have AWSConnector and AmazonS3FullAccess for
// this example to work
AWSCredentials credentials = new BasicAWSCredentials("AKIAJKQUCPNKR7SMLKHA", "fqt2QoaJSy4xE95lk+d29MoilYDk1MZj+Wjny7Qk");

// create a client connection based on credentials
AmazonS3 s3client = new AmazonS3Client(credentials);

// create bucket - name must be unique for all S3 users
String bucketName = "uploads.cdn.placester.net";
s3client.createBucket(bucketName);

// list buckets
for (Bucket bucket : s3client.listBuckets()) {
System.out.println(" - " + bucket.getName());
}

// create folder into bucket
String folderName = "testfolder";
createFolder(bucketName, folderName, s3client);

// upload file to folder and set it to public
String fileName = folderName + SUFFIX + "build.xml";
s3client.putObject(new PutObjectRequest(bucketName, fileName,
new File("E:\\jets jar\\jets3t-0.9.4\\build.xml"))
.withCannedAcl(CannedAccessControlList.PublicRead));

deleteFolder(bucketName, folderName, s3client);

// deletes bucket
s3client.deleteBucket(bucketName);
}

public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
// create meta-data for your folder and set content-length to 0
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
// create empty content
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// create a PutObjectRequest passing the folder name suffixed by /
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
folderName + SUFFIX, emptyContent, metadata);
// send request to S3 to create folder
client.putObject(putObjectRequest);
}
/**
* This method first deletes all the files in given folder and than the
* folder itself
*/
public static void deleteFolder(String bucketName, String folderName, AmazonS3 client) {
List fileList =
client.listObjects(bucketName, folderName).getObjectSummaries();
for (S3ObjectSummary file : fileList) {
client.deleteObject(bucketName, file.getKey());
}
client.deleteObject(bucketName, folderName);
}
}

No comments:

Post a Comment

Nature Vs Human

  प्रकृति से खिलवाड़ ना कर, वसुधा का अपमान ना कर, हे मानव उठ जाग, देख अपने कर्मों को, कितने दर्द दिये हैं तूने इस वसुधा को, मातृरुप इस धरती ...