问题
I'm coding a video sharing site. I'm using S3 to store and serve up the videos. I've coded tags for the videos in my MySQL database but I saw that S3 supports settings tags on uploaded files. Here's the code I'm using to upload files:
try {
//Create a S3Client
$s3Client = new S3Client([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => '',
'secret' => ''
]
]);
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $assetFilename,
'SourceFile' => $fileTmpPath,
'Metadata' => array(
'title' => $requestAr['title'],
'description' => $requestAr['description']
)
]);
$s3Client->waitUntil('ObjectExists', array(
'Bucket' => $bucket,
'Key' => $assetFilename
));
} catch (S3Exception $e) {
returnError($e->getMessage());
}
This is a 2 part question. First, how can I specify tags here? I can't find any PHP examples. Next, let's say someone uploads a video and they put a tag on it. S3 tags are key/value pairs. What should I set for the keys? I was thinking of just using something like [tag1, tag2, tag3] for each video and the value of those being the tag strings. Here's the S3 tags doc: https://docs.aws.amazon.com/AmazonS3/latest/dev/object-tagging.html
回答1:
To set an object tag, simply pass the Tagging
element to the putObject()
parameters. In your case, it'd be like this:
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $assetFilename,
'SourceFile' => $fileTmpPath,
'Tagging' => 'category=tag1', // your tag here!
'Metadata' => array(
'title' => $requestAr['title'],
'description' => $requestAr['description']
)
]);
Notice the tag is a simple key=value
string. As a "thinking ahead" measure, I'd make the tags be category=tagValue
, so you can categorize them and eventually add more tag categories. If you do tag1=true
, then it'll get messy quick.
- putObject() reference
来源:https://stackoverflow.com/questions/46248999/php-amazon-s3-uploads-and-tags