php aws_AWS-S3 PHP API示例

这篇博客展示了如何使用PHP AWS SDK进行S3对象上传、复制和列出文件列表的操作。`s3upload`函数用于上传文件,`s3copy`函数实现文件复制,`getFileListOfS3`函数获取指定前缀的文件列表,而`getMetaOfS3`函数则用于获取文件元数据。
摘要由CSDN通过智能技术生成

function s3upload($file){

global $s3, $bucket, $finfo;

$result = $s3->putObject([

'Body' => $file->content,

'Bucket' => $bucket,

'Key' => "dir/subdir/{$file->name}",

'ContentType' => $mimeType ? $mimeType : $finfo->buffer($file->content),

'Metadata' => [

'fileId' => $file->Id,

'size' => $file->fileSize,

]

]);

return $result;

}

function s3copy($fileName){

global $s3, $bucket;

$fromKey = "ab/$fileName";

$toKey = "cd/$fileName";

$result = $s3->copy($bucket, $fromKey, $bucket, $toKey);

return $result;

}

function getFileListOfS3($prefix, $withMeta = false){

global $s3, $bucket;

try {

$result = $s3->listObjects([

'Bucket' => $bucket,

'Prefix' => $prefix

]);

$list = (array)$result->get('Contents');

if ($withMeta) {

foreach ($list as $key => $item) {

$list[$key]['Meta'] = getMetaOfS3($item['Key'])->get('Metadata');

}

}

return $list;//return an array, each item contains a key 'Key', value of which is like 'dir1/dir2/sample.jpeg'

} catch (S3Exception $e) {

throw new \Exception($e->getMessage());

}

}

function getMetaOfS3($key){

global $s3, $bucket;

try {

$result = $s3->headObject([

'Bucket' => $bucket,

'Key' => $key,

]);

return $result;

} catch(S3Exception $e) {

throw new \Exception($e->getMessage());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值