我需要在我的wordpress博客上显示上传到我的Amazon S3账户的ACL:PRIVATE的视频/图像文件。
我是基于 PHP oops 的编码的新手。任何脚本帮助,链接引用,免费插件甚至逻辑算法都将是很大的帮助:)
提前谢谢。
可以通过实施以下步骤来解决此问题:
- 从这里下载最新的稳定版本的SDK 浏览器
- 解压缩.zip文件并放入
wamp/www
文件夹中 - 将 config-sample.inc.php 文件重命名为 config.inc.php
- 将访问密钥和私有密钥(从 Amazon S3 账户检索)添加到上述文件中,保存并退出
- 创建示例文件以显示来自 Amazon S3 的公有/私有对象
文件的内容应如下所示:
require('sdk.class.php');
require('services/s3.class.php');
$s3 = new AmazonS3();
$bucket = "bucketname";
$temp_link = $s3->get_object_url($bucket, 'your/folder/path/img.jpg', '5 minute');
echo $temp_link;
在上面的代码中,您作为输出收到的 URL 是私有对象的签名 URL,因此它仅在 5 分钟内有效。
您可以授予未来日期的访问权限,并仅允许授权用户访问您在 Amazon S3 上的私有内容或媒体。
这个问题有点旧了,但我还是发布了它。我今天遇到了一个简单的问题,发现有一个简单的答案。
AWS 文档对此进行了清晰的解释,并提供了一个示例。
http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/service-s3.html#amazon-s3-stream-wrapper
基本上,您需要注册AWS的流包装器并使用s3://协议。
这是我的代码示例。
use Aws'Common'Aws;
use Aws'S3'Enum'CannedAcl;
use Aws'S3'Exception'S3Exception;
$s3 = Aws::factory(array(
'key' => Config::get('aws.key'),
'secret' => Config::get('aws.secret'),
'region' => Config::get('aws.region')
))->get('s3');
$s3->registerStreamWrapper();
// now read file from s3
// from the doc.
// Open a stream in read-only mode
if ($stream = fopen('s3://bucket/key', 'r')) {
// While the stream is still open
while (!feof($stream)) {
// Read 1024 bytes from the stream
echo fread($stream, 1024);
}
// Be sure to close the stream resource when you're done with it
fclose($stream);
}