【问题标题】:How to connect CloudFront in java? [closed]如何在 Java 中连接 CloudFront? [关闭]
【发布时间】:2020-02-02 10:00:37
【问题描述】:

目前,我正在使用 java API 使用 Amazon s3 SDK 将文档上传到 Amazon s3,但我想利用 CloudFront 的缓存工具来加快上传下载过程,因此任何人都可以提供任何关于如何上传文件的指示/使用 Cloudfront 的 s3 中的图像?

【问题讨论】:

    标签: java amazon-s3 file-upload amazon-cloudfront cdn


    【解决方案1】:

    终于,我得到了答案。我正在使用 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();
        }
    

    【讨论】:

    • 你是如何上传到这里的 s3 存储桶的?
    • 嘿@TheNightsWatch,首先将cdn url配置为s3存储桶,然后使用带有cdn url的Http post api(指向带有文件夹的存储桶)您可以上传文档。
    • @MansiJoshi ,我不知道我是否应该问这个问题,但是你的项目是公开的吗?那会更有帮助。
    • @MansiJoshi 你能帮我解决这个问题吗?我想通过 CloudFront 获取我的视频并将其发布到 AWS s3 存储桶。
    • 嘿,@Amit 我的项目不是公开的,对于您通过 Cloud Front 将视频发布到 s3 中,您可以使用上述方法并从云端获取对象/视频只需使用 **cloudfront URL/您的视频路径在 s3 中**。
    猜你喜欢
    • 2013-02-20
    • 2010-09-21
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2015-04-28
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多