android 如何使用来自 aws S3 服务器的 presignedurl 上传图像

How to upload image using presignedurl from aws S3 server in android

我正在尝试将图像从预签名的 url 上传到 S3 存储桶,它给出了 ssl 异常错误连接被对等方关闭

这是我的代码

public int upload(String filePath, URL url) {
 Bitmap bm = BitmapFactory.decodeFile(filePath);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
            byte[] fileBytes = bos.toByteArray();

            connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/octet-stream"); 

            OutputStream output = connection.getOutputStream();
            InputStream input = new ByteArrayInputStream(fileBytes);
            byte[] buffer = new byte[4096];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();

            return connection.getResponseCode();
}

我终于明白了, 这是使用预签名 URL

将图像发送到 S3 的代码
try {

            Bitmap bm = BitmapFactory.decodeFile(fileBytes);
            connection = (HttpsURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("PUT");
            connection.setRequestProperty("Content-Type", "application/octet-stream"); // Very important ! It won't work without adding this!

            OutputStream output = connection.getOutputStream();


            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 50, output);
            output.flush();

            int response = connection.getResponseCode();

            return connection.getResponseCode();
        } catch (IOException e) {
            e.printStackTrace();
        }