繁体   English   中英

在亚马逊s3上上传图像显示TransferUtility尚未使用默认存储桶android配置

[英]uploading image on amazon s3 shows TransferUtility has not been configured with a default bucket android

我正在尝试将相机或图库图像上传到亚马逊s3。我具有存储桶名称,访问密钥,秘密密钥,文件名和位置。请帮助我使用这些凭据在亚马逊s3上上传图像。 我收到以下例外情况。请帮助我

java.lang.IllegalArgumentException: TransferUtility has not been configured with a default bucket. Please use the corresponding method that specifies bucket name or configure the default bucket name in construction of the object. See TransferUtility.builder().defaultBucket() or TransferUtility.builder().awsConfiguration()

这是我的代码

///////////////////////////////////////UPLOAD TO S3 WITH KEY AND SECRETKEY///////////////////
        // KEY and SECRET are gotten when we create an IAM user above
        AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("city", cityName);
        AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("type", entityType);
        AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("entityid", mallId);
        AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("fileName", Common.imageName);
        Log.d("S3 KEY", AWSCredentials.S3KEY);

        BasicAWSCredentials credentials = new BasicAWSCredentials(AWSCredentials.KEY, AWSCredentials.SECRET);
        AmazonS3Client s3Client = new AmazonS3Client(credentials);
        s3Client.putObject(new PutObjectRequest(AWSCredentials.BUCKET_NAME, AWSCredentials.KEY, new File(Common.imageLocation)));
        //s3Client.setBucketAccelerateConfiguration(AWSCredentials.BUCKET_NAME);
        //s3Client.setEndpoint("https://s3.ap-south-1.amazonaws.com/lipl-media/");
        TransferUtility transferUtility = TransferUtility.builder()
                .context(getApplicationContext())
                .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                .s3Client(s3Client)
                .build();

/*try
{
    // FileInputStream fis = new FileInputStream(new 
 File(Common.imageName));
}
 catch(FileNotFoundException e)
{
e.printStackTrace();
 }*/
             TransferObserver uploadObserver =
                transferUtility.upload(AWSCredentials.S3KEY, new File(Common.imageLocation));

        uploadObserver.setTransferListener(new TransferListener() {

            @Override
            public void onStateChanged(int id, TransferState state) {
                if (TransferState.COMPLETED == state) {
                    //Toast.makeText(FormBuilderActivity.this,"Upload Status:"+state.toString(),Toast.LENGTH_SHORT).show();
                    Log.d("S3 upload status", state.toString());
                    // Handle a completed download.
                }
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                float percentDonef = ((float)bytesCurrent/(float)bytesTotal) * 100;
                int percentDone = (int)percentDonef;
            }

            @Override
            public void onError(int id, Exception ex) {
                //Toast.makeText(FormBuilderActivity.this,"Upload Error:"+ex.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
                Log.d("S3 upload fail", ex.getLocalizedMessage());
                ex.printStackTrace();
                // Handle errors
            }

        });
 // If your upload does not trigger the onStateChanged method inside your
 // TransferListener, you can directly check the transfer state as shown here.
        if (TransferState.COMPLETED == uploadObserver.getState()) {
            Log.d("S3 upload status", uploadObserver.getState().toString());
            //Toast.makeText(FormBuilderActivity.this,"Upload Status:"+uploadObserver.getState().toString(),Toast.LENGTH_SHORT).show();
 // Handle a completed upload.
        }

下面的行导致该错误。

TransferObserver uploadObserver =
                transferUtility.upload(AWSCredentials.S3KEY, new File(Common.imageLocation));

这是因为未为TransferUtility实例指定目标存储桶。 而是使用下面的代码,它是TransferUtility类的另一个构造函数,其中将存储桶作为参数传递。

TransferObserver uploadObserver =
                transferUtility.upload(YOUR_BUCKET_NAME, AWSCredentials.S3KEY, new File(Common.imageLocation));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM