为了账号安全,请及时绑定邮箱和手机立即绑定

如何用java连接CloudFront?

如何用java连接CloudFront?

蝴蝶刀刀 2023-09-27 10:29:00
目前,我正在使用 java API 使用 Amazon s3 SDK 将文档上传到 Amazon s3,但我想利用 CloudFront 的缓存设施来加速上传下载过程,因此任何人都可以提供有关如何在 s3 中上传文件/图像的任何指示使用云前端?
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

最后,我得到了答案。我正在使用 CloudFront Http 方法通过 CloudFront 在 S3 存储桶中上传图像/文档。这是我的代码


URL url;

    try {

        url = new URL("**cloudfront URL***"+imagePath);

          HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);

            connection.setDoInput(true);

            connection.setRequestMethod("PUT");

            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

            connection.setRequestProperty("charset","UTF-8");

            connection.setRequestProperty("Content-Length",imageByteArray.length+"");

            DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());

            wr.write(imageByteArray);

            wr.flush();

            wr.close();

            connection.disconnect();

            // Check the HTTP response code. To complete the upload and make the object available, 

            // you must interact with the connection object in some way.

            connection.getResponseCode();

            System.out.println("HTTP response code: " + connection.getResponseCode());

    } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }


查看完整回答
反对 回复 2023-09-27
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信