S3使用TransferManager上传/下载

时间:2017-05-03 06:03:13

标签: java amazon-web-services amazon-s3 amazon-ec2

我正在尝试编写一个快速代码片段,使用TransferManager API从/向S3下载/上传JAR ...我不断收到此300 Bad Reuqest错误消息。 有人可以告诉我,如果我可能在这里遗漏任何东西

package com.mycompany.app;

    import java.io.File;
    import java.util.concurrent.Executors;

    import com.amazonaws.AmazonClientException;
    import com.amazonaws.AmazonServiceException;
    import com.amazonaws.auth.AWSCredentials;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.event.ProgressEvent;
    import com.amazonaws.event.ProgressListener;
    import com.amazonaws.regions.Regions;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3ClientBuilder;
    import com.amazonaws.services.s3.model.Bucket;
    import com.amazonaws.services.s3.transfer.Download;
    import com.amazonaws.services.s3.transfer.TransferManager;
    import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
    import com.amazonaws.services.s3.transfer.Upload;

    /**
     * Hello world!
     *
     */
    public class App {

        private static Upload upload;
        private static Download download;
        private static BasicAWSCredentials creds = new BasicAWSCredentials("XXXXXX",
                "XXXXXXX");
        private static AmazonS3 client = AmazonS3ClientBuilder.standard()
                .withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(creds)).build();

        public static void WaitForCompletion(boolean isUpload)
                throws AmazonServiceException, AmazonClientException, InterruptedException {

            if (isUpload && upload != null) {
                System.out.println("Waiting for Upload to Complete ...");
                upload.waitForCompletion();
            } else if (!isUpload && download != null) {
                System.out.println("Waiting for Download to Complete ...");
                download.waitForCompletion();
            }
        }

        public static void UploadFile(String bucketName, File targetFile) {



            TransferManager tm = TransferManagerBuilder.standard()
                    .withExecutorFactory(() -> Executors.newFixedThreadPool(50)).withS3Client(client).build();

            try {
                ProgressListener pl = new ProgressListener() {
                    public void progressChanged(ProgressEvent progressEvent) {
                        System.out.println("Progress Event: " + progressEvent.getEventType());
                    }
                };

                ProgressListener.ExceptionReporter er = ProgressListener.ExceptionReporter.wrap(pl);
                upload = tm.upload(bucketName, targetFile.getName(), targetFile);
                upload.addProgressListener(er);
                WaitForCompletion(true);

            } catch (AmazonServiceException ase) {
                System.out.println("Service Exception Catched : " + ase.getLocalizedMessage());
                throw ase;
            } catch (AmazonClientException ace) {
                System.out.println("Client Exception Catched : " + ace.getLocalizedMessage());
            } catch (Exception e) {
                System.out.println("Exception Catched " + e.getLocalizedMessage());
            } finally {
                if (tm != null) {
                    tm.shutdownNow();
                }
            }

        }

        public static void DownloadFile(String bucketName, String keyFileName) {


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

            TransferManager tm = TransferManagerBuilder.standard()
                    .withExecutorFactory(() -> Executors.newFixedThreadPool(50)).withS3Client(client).build();

            try {
                ProgressListener pl = new ProgressListener() {
                   public void progressChanged(ProgressEvent progressEvent) {
                      System.out.println("Progress Event: " + progressEvent.getEventType());
                  }
               };

                ProgressListener.ExceptionReporter er = ProgressListener.ExceptionReporter.wrap(pl);
                download = tm.download(bucketName, keyFileName, new File("tmp.jar"));
                download.addProgressListener(er);
                WaitForCompletion(false);

            } catch (AmazonServiceException ase) {
                System.out.println("Service Exception Catched : " + ase.getLocalizedMessage());
                ase.printStackTrace();
                ase.getErrorMessage();

                throw ase;
            } catch (AmazonClientException ace) {
                System.out.println("Client Exception Catched : " + ace.getLocalizedMessage());
            } catch (Exception e) {
                System.out.println("Exception Catched " + e.getLocalizedMessage());
            } finally {
                if (tm != null) {
                    tm.shutdownNow();
                }
            }
        }

        public static void main(String[] args) {

            DownloadFile("devbuck","commons.jar");

        }
    }

0 个答案:

没有答案