AWS SDK Java

使用 AWS 官方 SDK aws-sdk-java

import java.io.File;
import java.util.UUID;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
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.S3ClientOptions;
import com.amazonaws.services.s3.S3ClientOptions.Builder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
import com.amazonaws.services.s3.transfer.model.UploadResult;

/*
 * This Java source file was auto generated by running 'gradle buildInit --type java-library'
 * by 'MC' at '10/24/17 11:07 AM' with Gradle 2.14.1
 *
 * @author MC, @date 10/24/17 11:07 AM
 */
public class S3 {
    public static void main(String[] args) {
        String s3endpoint = "https://api-s3.qiniu.com";
        String s3bucket = "<your-qiniu-bucket-name>";
        String accessKeyId = "<your-qiniu-access-key-id>";
        String accessKeySecret = "<your-qiniu-access-secret-key>";

        // credentials
        AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, accessKeySecret);

        Builder options = S3ClientOptions.builder();
        options.setPathStyleAccess(true);
        options.setAccelerateModeEnabled(false);
        options.setPayloadSigningEnabled(true);
        // options.disableChunkedEncoding();

        AmazonS3 s3 = new AmazonS3Client(credentials);
        s3.setEndpoint(s3endpoint);
        s3.setS3ClientOptions(options.build());

        try {
            /*
             * Create a new S3 bucket - Amazon S3 bucket names are globally
             * unique, so once a bucket name has been taken by any user, you
             * can't create another bucket with that same name.
             *
             * You can optionally specify a location for your bucket if you want
             * to keep your data closer to your applications or users.
             */
            // System.out.println("Creating bucket " + s3bucket + "\n");
            // Bucket bkt = s3.createBucket(s3bucket);
            // System.out.println(bkt.toString());

            /*
             * List the buckets in your account
             */
            System.out.println("Listing buckets ...");

            for (Bucket bucket : s3.listBuckets()) {
                System.out.println(" - " + bucket.getName());
            }
            System.out.println();

            /*
             * Upload an object to your bucket - You can easily upload a file to
             * S3, or upload directly an InputStream if you know the length of
             * the data in the stream. You can also specify your own metadata
             * when uploading to S3, which allows you set a variety of options
             * like content-type and content-encoding, plus additional metadata
             * specific to your applications.
             */
            System.out.println("Uploading a new object to S3 from local file ...");

            String s3object = UUID.randomUUID().toString();
            PutObjectResult putResult = s3
                    .putObject(new PutObjectRequest(s3bucket, s3object, new File("/path/to/file")));
            System.out.println("Put object " + s3object + " Etag: " + putResult.getETag());
            System.out.println();

            /*
             * Multipart upload
             */
            System.out.println("Multipart uploading ...");

            TransferManagerBuilder tmb = TransferManagerBuilder.standard();
            tmb.setMinimumUploadPartSize((long) (4 << 20)); // force part size to 4M or 4M * N
            tmb.setS3Client(s3);
            tmb.setMultipartUploadThreshold((long) 10);

            File file = new File("/path/to/file");

            TransferManager tm = tmb.build();
            Upload upload = tm.upload(s3bucket, file.getName(), file);
            UploadResult tmResult = upload.waitForUploadResult();
            System.out.println(tmResult.toString());

        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon S3, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());

        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with S3, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());

        } catch (Throwable ie) {
            System.out.println(ie.getMessage());

        }

    }

}

results matching ""

    No results matching ""