我想为我的asp.net Web应用程序存储在S3中的文件设置过期日期标题。
答案 0 :(得分:19)
当您使用Asp.net时,我假设您使用的是AWS .NET SDK。
要在上传对象时添加Expires
(或任何其他http标头),请将其添加为PutObject
请求的一部分。
var client = new Amazon.S3.AmazonS3Client(AWS_Key, AWS_SecretKey);
var req = new Amazon.S3.Model.PutObjectRequest()
.WithFilePath(@"C:\myfile.txt")
.WithKey("myfile.txt")
.WithBucketName("mybucket");
req.AddHeader("expires", "Thu, 01 Dec 1994 16:00:00 GMT");
client.PutObject(req);
要更改现有对象的标题,您需要将对象复制到自身。
var req = new Amazon.S3.Model.CopyObjectRequest()
.WithDirective(Amazon.S3.Model.S3MetadataDirective.REPLACE)
.WithSourceBucket("mybucket")
.WithSourceKey("myfile.txt")
.WithDestinationBucket("mybucket")
.WithDestinationKey("myfile.txt");
req.AddHeader("expires", "Thu, 01 Dec 1994 16:00:00 GMT");
client.CopyObject(req);
注意:必须设置.WithDirective(Amazon.S3.Model.S3MetadataDirective.REPLACE)
才能指定新标头。否则,只复制现有的标题。
更多信息请参阅.NET SDK docs。
答案 1 :(得分:7)
如果您使用的是 AWS SDK 2.X ,那么" AddHeader"方法不再可用。要添加标题,只需直接修改标题集合即可。
req.Headers["expires"] = "Thu, 01 Dec 1994 16:00:00 GMT";
以下是Geoff使用的修改示例:
var client = new Amazon.S3.AmazonS3Client(AWS_Key, AWS_SecretKey);
var req = PutObjectRequest req= new PutObjectRequest()
{
BucketName = "mybucket",
Key = "myfile.txt",
FilePath = @"C:\myfile.txt"
};
req.Headers["expires"] = "Thu, 01 Dec 1994 16:00:00 GMT";
client.PutObject(req);
要以相同的方式更改标题:
var req = new Amazon.S3.Model.CopyObjectRequest()
{
MetadataDirective = S3MetadataDirective.REPLACE,
SourceBucket = "mybucket",
SourceKey = "myfile.txt",
DestinationBucket = "mybucket",
DestinationKey = "myfile.txt"
};
req.Headers["expires"] = "Thu, 01 Dec 1994 16:00:00 GMT";
client.CopyObject(req);
答案 2 :(得分:2)
对于AWSSDK.S3 V3。*它的工作原理如下:
{{1}}