2014-05-08 31 views
0

我正在将文件上传到已为其创建Cloudfront发行版的S3存储桶。我正在使用Aws\S3\S3Client类。如何在S3上传后返回Cloudfront网址?

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

有什么办法直接得到这个URL,还是我必须从我的分发主机名和文件路径来构建它?

回答

3

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

只返回CloudFront的URL中的文件路径e.g面前......

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

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

return 'https://d111111111ck.cloudfront.net' . $filePath; 
+0

没错,所以我想没有办法解决存储桶的关联云端网址。我只需要将它存储在配置中。 – juuga

+1

这是正确的,您可以使用'Aws \ CloudFront \ CloudFrontClient'类中的'getDistribution'方法来返回域,但您必须知道该ID,以便您可以将域存储在配置中! – fire

+0

我得到了这个错误“MissingKey” – NineCattoRules

0

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

CloudFrontCDN缓存。您无需获取s3存储桶/文件的URL,因为CloudFront会为您提供帮助。

更多细节AWS CloudFront here。

按照步骤操作并查看step 24。而已!下面

截图:

enter image description here enter image description here

享受! :)