将Android文件上传到Amazon S3
问题描述:
我想将图像上传到我的s3存储桶。我尝试了不同的解决方案来上传文件,如this tutorial,但我总是得到错误的SSL例外:将Android文件上传到Amazon S3
Unable to execute HTTP request: Write error: ssl=0x55a53d5240: I/O error during system call, Connection reset by peer
我发现有关此异常的评论一个评论是,如果你的网络不好,你可以在错误得到。但我的网络很好。
我的代码如下:
private void uploadImage() {
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getContext().getApplicationContext(),
"us-west-2:************", // Identity Pool ID
Regions.US_WEST_2 // Region
);
ClientConfiguration configuration = new ClientConfiguration();
configuration.setConnectionTimeout(50000);
configuration.setSocketTimeout(300000);
final AmazonS3 s3 = new AmazonS3Client(credentialsProvider, configuration);
TransferUtility transferUtility = new TransferUtility(s3, getContext().getApplicationContext());
LocalImage image = mAddImageAdapter.getLocalImage(0);
final File mFile = new File(image.getImagePath());
long len = mFile.length();
transferUtility.upload("2y1s-images", mFile.getName(), mFile).setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
Log.i(TAG, "image upload state: " + state.name());
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
Log.e(TAG, "image upload percentage: " + bytesCurrent + " -" + bytesTotal);
}
@Override
public void onError(int id, Exception ex) {
Log.e(TAG, "image upload error: " + ex.getMessage());
}
});
}
答
我发现花了2天之后异常的原因。 ssl例外的主要原因是我在不同地区的认证凭证和s3桶。我希望这个答案有助于其他人。
只是检查 - 您是否记得在您的清单中添加互联网许可? – SteelBytes
是的,我有互联网,读取和写入权限 – gokhan
哪些版本的aws sdk你使用? (最新的?) – SteelBytes