android - 尝试上传到 AWS s3 时出现 SSL 异常

标签 android ssl amazon-web-services amazon-s3

当尝试在我的 Android 应用程序上上传图片时,我收到此错误。这是我第一次使用 AWS。

Unable to execute HTTP request: Write error: ssl=0x9f025e00: I/O error during system call, Connection reset by peer
javax.net.ssl.SSLException: Write error: ssl=0x9f025e00: I/O error during system call, Connection reset by peer

我已经创建了一个帐户、一个存储桶、一个 Cognito 身份池。

public class SaveProfilePictureOnAWSS3 extends AsyncTask<String, Void, Void> {
    @Override
    public Void doInBackground(String... params) {

        try {

            String picture_url = params[0];

            // Initialize the Amazon Cognito credentials provider
            CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    App.getInstance().getApplicationContext(), // Application Context
                    App.getInstance().getResources().getString(R.string.AWS_IdentityPoolId), // Identity Pool ID
                    Regions.EU_WEST_1 // Region enum
            );

            AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);

            File fileToUpload = new File(picture_url);

            PutObjectRequest putRequest = new PutObjectRequest("millezimu", MilleZimU.getInstance().getUserInstance().getId(), fileToUpload);
            PutObjectResult putResponse = s3Client.putObject(putRequest);



        } catch (Exception e) {
            EventBus.getDefault().post(new MessageToDisplayEvent(e.getMessage(), true));
        }


        return null;
    }

或者这样:

TransferUtility transferUtility = AWSUtils.getTransferUtility(MilleZimU.getInstance());
TransferObserver observer = transferUtility.upload("millezimu",
    MilleZimU.getInstance().getUserInstance().getId(), fileToUpload);

但仍然出现错误!

最佳答案

出现这个问题是因为客户端的区域没有设置!!!

将区域添加到 Amazon Demo 中提供的帮助程序后,它就可以工作了。

/**
 * Gets an instance of a S3 client which is constructed using the given
 * Context.
 *
 * @param context An Context instance.
 * @return A default S3 client.
 */
public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
        sS3Client.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));
    }
    return sS3Client;
}

关于android - 尝试上传到 AWS s3 时出现 SSL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33310893/

相关文章:

Android - 使用 QFile 打开文件

Postgresql - 100% 确定使用 SSL 连接服务器?

php - 错误 60 : SSL certificate issue with Youtube Api v3

android - 如何在android中的日期中添加天数

android - 在 Android 版 Chrome 上禁用动量滚动

java - Android 谷歌地图 API 无法正常工作

security - 为什么浏览器接受通过非安全 (HTTP) 连接发送的安全 cookie?

amazon-web-services - Amazon Simple Email Service 将于 2020 年 9 月 30 日终止对签名版本 3 的支持

amazon-web-services - AWS Cloud9 - 设备上没有剩余空间

javascript - 封包加密大文件