【问题标题】:How do I return Cloudfront url after S3 upload?S3 上传后如何返回 Cloudfront url?
【发布时间】:2017-05-04 06:25:21
【问题描述】:

我正在将文件上传到我已为其创建 Cloudfront 分发的 S3 存储桶。我正在使用Aws\S3\S3Client 类。

使用putObject上传后,响应对象和getObjectUrl方法都返回对象的url为https://s3-eu-west-1.amazonaws.com/mybucket/path/myfile.jpg。我正在尝试获取类似于 https://d111111111ck.cloudfront.net/path/myfile.jpg 的 Cloudfront url。

有什么方法可以直接获取这个 url,还是我必须从我的分发主机名和文件路径构建它?

【问题讨论】:

    标签: php amazon-web-services amazon-s3


    【解决方案1】:

    您不会从 S3 获得云端 URL,它是一项不同的服务。如果您使用putObject,那么您已经知道文件路径(Key 中指定的值)。

    只需在文件路径前面返回云端 URL,例如...

    $filePath = '/path/file.jpg';
    
    $client->putObject(array(
        'Bucket' => 'mybucket',
        'Key' => $filePath,
        'SourceFile' => $fileSource,
        'ACL' => 'public-read'
    ));
    
    return 'https://d111111111ck.cloudfront.net' . $filePath;
    

    【讨论】:

    • 对,所以我想没有办法为存储桶解析关联的云端 url。我只需要将它存储在配置中。
    • 没错,你可以使用Aws\CloudFront\CloudFrontClient类中的getDistribution方法返回域,但你必须知道ID,所以你也可以将域存储在config中!跨度>
    【解决方案2】:

    I guess there's is no way to resolve an associated cloudfront url for the bucket。是的,是的!

    CloudFront 是一种CDN 缓存。您无需获取 s3 存储桶/文件的 URL,因为 CloudFront 会为您解决这个问题。

    更多关于AWS CloudFront的细节在这里。

    按照步骤操作并查看step 24。就是这样!

    截图如下:

    享受吧! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 2017-08-02
      • 2018-05-07
      • 1970-01-01
      • 2015-11-17
      • 2018-12-30
      相关资源
      最近更新 更多