Saturday, April 4, 2015

Amazon S3

S3 cơ bn

Link tham khảo
http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-setup.html#java-dg-java-env
1.cài sdk của Amazon vào eclipse:
help / install new software , sử dụng link bên dưới
http://aws.amazon.com/eclipse/site.xml
2. Sử dụng project maven
http://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.9.6
3.Name
bucketName: tên của thư mục gốc, vd: /abc/aaa/1.txt, thì bucket name là "abc", còn key sẽ là "aaa/1.txt"
4.Check sum MD5
file khi được up lên thành công trong đối tượng trả về có chứa md5 check sum của file, vậy cần tạo check sum trước khi up file và so sánh với check sum trả về của file nếu trùng nhau thì đã upload file lên s3 thành công

    com.amazonaws
    aws-java-sdk
    1.9.6
5.Maven lại project
package com.testS3;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.event.ProgressEvent;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;

public class S3Utils {
   
    private AWSCredentials credentials;
    private AmazonS3 s3Client = new AmazonS3Client(credentials);
    private Region region = Region.getRegion(Regions.AP_NORTHEAST_1);
   
    public void setAuthenticate(String accessKey, String secretKey){
        credentials = new BasicAWSCredentials(accessKey, secretKey);
        s3Client = new AmazonS3Client(credentials);
        s3Client.setRegion(region);
    }

    public AmazonS3 getAmazonS3Client() {
        return s3Client;
    }

    public AWSCredentials getAWSCredentials(String accessKey, String secretKey) {
        return credentials;
    }
    /**
     * upload file, return Result check md5 file
     * @param bucketName
     * @param key
     * @param input
     * @return
     * @throws Exception
     */
    public boolean saveFile(String bucketName, String key, String input)
            throws Exception {

        byte[] resultByte = DigestUtils.md5(new FileInputStream(input));
        String streamMD5 = new String(Base64.encodeBase64(resultByte));

        PutObjectRequest request = new PutObjectRequest(bucketName, key,
                new File(input));
        PutObjectResult putObjectResult = s3Client.putObject(request);

        return putObjectResult.getContentMd5().equals(streamMD5);
    }
    /**
     * downLoad File, save file to output path
     * @param bucketName
     * @param key
     * @param output
     */
    public void downLoadFile(String bucketName, String key, String output) {
        s3Client.getObject(new GetObjectRequest(bucketName, key), new File(
                output));
    }
   
    /**
     * deleteFile
     * @param bucketName
     * @param key
     */
    public void deleteFile(String bucketName, String key) {
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(
                bucketName, key);
        s3Client.deleteObject(deleteObjectRequest);
    }
   
    /**
     * get list object in bucket
     * @param bucketName
     * @return
     */
    public List getListObjects(String bucketName) {
        ObjectListing objectListing = s3Client
                .listObjects(new ListObjectsRequest()
                        .withBucketName(bucketName));
        return objectListing.getObjectSummaries();
    }
   
    /**
     * uploadMultipartFile
     * @param BucketName
     * @param key
     * @param input
     * @throws InterruptedException
     */
    public void uploadMultipartFile(String BucketName, String key, String input) throws InterruptedException{
        TransferManager tm = new TransferManager(credentials);

        // For more advanced uploads, you can create a request object
        // and supply additional request parameters (ex: progress listeners,
        // canned ACLs, etc.)
        PutObjectRequest request = new PutObjectRequest(
                BucketName, key, new File(input));
       
        // You can ask the upload for its progress, or you can
        // add a ProgressListener to your request to receive notifications
        // when bytes are transferred.
        request.setGeneralProgressListener(new ProgressListener() {
            public void progressChanged(ProgressEvent progressEvent) {
                System.out.println("Transferred bytes: " +
                        progressEvent.getBytesTransferred());
            }
        });

        // TransferManager processes all transfers asynchronously,
        // so this call will return immediately.
        Upload upload = tm.upload(request);
       
        try {
            // You can block and wait for the upload to finish
            upload.waitForCompletion();
        } catch (AmazonClientException amazonClientException) {
            System.out.println("Unable to upload file, upload aborted.");
            amazonClientException.printStackTrace();
        }
    }
    /**
     * deleteFolder
     * @param bucketName
     * @param key
     */
    public void deleteFolder(String bucketName, String key){
        List keys = new ArrayList();
        KeyVersion keyVersion = null;

        ObjectListing list = s3Client.listObjects(bucketName, key);
        do {
            List summaries = list.getObjectSummaries();
            for (S3ObjectSummary summary : summaries) {
                keyVersion = new KeyVersion(summary.getKey());
                keys.add(keyVersion);
            }
            list = s3Client.listNextBatchOfObjects(list);
        } while (list.isTruncated());
        if (keys.size() > 0){
            DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
            s3Client.deleteObjects(deleteObjectsRequest.withKeys(keys));
        }
    }
}


No comments:

Post a Comment