简体   繁体   English

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

[英]Getting SSL Exception with trying upload to AWS s3

I get this error, when try to upload a picture on my Android app.尝试在我的 Android 应用上上传图片时出现此错误。 This is the first time I'm working with AWS.这是我第一次与 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

I have create a Account, a Bucket, a Cognito Identity Pool.我创建了一个帐户、一个存储桶、一个 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;
    }

or with this :或与此:

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

but still get the error !但仍然得到错误!

The problem happen because the region of the client was not set !!!问题发生是因为客户端的区域没有设置!!!

After adding the region to the helper provided in Amazon Demo, it works.将区域添加到 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;
}

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

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