我正在尝试将相机或图库图像上传到亚马逊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.
}
答案 0 :(得分:1)
以下行导致错误。
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));