S3上传后如何返回Cloudfront url?

juu*_*uga 2 php amazon-s3 amazon-web-services

我正在将文件上传到我为其创建了 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,还是必须从我的分发主机名和文件路径中构建它?

fir*_*ire 6

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

只需在文件路径前返回 cloudfront URL,例如..

$filePath = '/path/file.jpg';

$client->putObject(array(
    'Bucket' => 'mybucket',
    'Key' => $filePath,
    'SourceFile' => $fileSource,
    'ACL' => 'public-read'
));

return 'https://d111111111ck.cloudfront.net' . $filePath;
Run Code Online (Sandbox Code Playgroud)