我们使用Amazon S3为我们的项目保存图片,但现在我们需要在S3中将一些图像从一个地方复制到另一个地方。我有较旧/保存的图像路径存储在我的数据库中,现在想要使用新路径保存它们。
有人可以指导我从任何正确的方向开始。我一直在调查CopyObjectRequest
,但无法弄清楚如何继续。
任何帮助将不胜感激。
由于
答案 0 :(得分:1)
这是一个通用示例,original
和destination
变量从调用者传递给函数。
public void DuplicateFileInCloud(string original, string destination)
{
try
{
CopyObjectRequest request = new CopyObjectRequest();
if (original.StartsWith("http"))
{
// example: http://jk-v30.s3.amazonaws.com/PredefinedFiles/Favicons/002.ico
string bucket = getBucketNameFromUrl(original), // i.e. jk-v30
key = getKeyFromUrl(original); // the path to your file: PredefinedFiles/Favicons/002.ico
request.WithSourceBucket(bucket);
request.WithSourceKey(key);
}
else
{
// same bucket: copy/paste operation
request.WithSourceBucket(this.bucketName);
request.WithSourceKey(original);
}
request.WithDestinationBucket(this.bucketName);
request.WithDestinationKey(destination);
request.CannedACL = S3CannedACL.PublicRead; // one way of setting public headers - see other below
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(this.accessKey, this.secretAccessKey))
{
S3Response response = client.CopyObject(request);
response.Dispose();
}
}
catch (AmazonS3Exception s3Exception)
{
throw s3Exception;
}
}
希望这就是你要找的东西!
设置public-read
response.Addheader("x-amz-acl","public-read");