S3对象在使用Golang SDK时未过期

huangapple go评论86阅读模式
英文:

S3 objects not expiring using the Golang SDK

问题

使用AWS Golang SDK,我正在尝试为我上传的一些对象设置过期日期。我相当确定头部已经正确设置,但是当登录到S3并查看新对象的属性时,它似乎没有过期日期。

以下是我上传对象的代码片段:

exp := time.Now()
exp = exp.Add(time.Hour * 24)

svc := s3.New(session.New(config))
_, err = svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String("MyBucketName"),
Key: aws.String("201700689.zip"),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
Expires: &exp,
})

这是我登录网站时看到的情况:

S3对象在使用Golang SDK时未过期

这里发生了什么问题?谢谢。

英文:

Using the AWS Golang SDK, I'm attempting to set an expiration date for some of the objects that I'm uploading. I'm pretty sure that the header is being set correctly, however, when logging into S3 and viewing the properties of the new object, it doesn't appear to have a expiration date.

Below is a snippet of how I'm uploading objects

exp := time.Now()
exp = exp.Add(time.Hour * 24)

svc := s3.New(session.New(config))
_, err = svc.PutObject(&s3.PutObjectInput{
	Bucket:        aws.String("MyBucketName"),
	Key:           aws.String("201700689.zip"),
	Body:          fileBytes,
	ContentLength: aws.Int64(size),
	ContentType:   aws.String(fileType),
	Expires:       &exp,
})

And here is what I see when logging into the site
S3对象在使用Golang SDK时未过期

Any idea what is going on here? Thanks

答案1

得分: 8

Expires字段是错误的:

> // 对象不再可缓存的日期和时间。

你需要的是对象过期,可以通过设置存储桶规则而不是每个对象来实现。

基本上,你可以在存储桶属性中添加一个生命周期规则,指定以下内容:

> 每个规则具有以下属性:
>
> 前缀 - 键名的初始部分(例如logs/)或整个键名。存储桶中具有匹配前缀的任何对象都将受到此过期规则的影响。空前缀将匹配存储桶中的所有对象。
>
> 状态 - 启用或禁用。你可以选择在某些时间启用规则以执行删除或垃圾回收操作,并在其他时间禁用规则。
>
> 过期 - 指定对象从创建日期起的过期期限,以天数表示。
>
> ID - 可选,为规则指定一个名称。

系统将每天评估此规则,并删除任何过期的对象。

请参阅https://aws.amazon.com/blogs/aws/amazon-s3-object-expiration/以获取更详细的解释。

英文:

Well, Expires is just the wrong field:

> // The date and time at which the object is no longer cacheable.

What you want is Object Expiration which can be set as a bucket rule and not per object.

Basically, you add a Lifecycle rule (on the bucket properties) specifying:

> Each rule has the following attributes:
>
> Prefix – Initial part of the key name, (e.g. logs/), or the entire key name. Any object in the bucket with a matching prefix will be subject to this expiration rule. An empty prefix will match all objects in the bucket.
>
> Status – Either Enabled or Disabled. You can choose to enable rules from time to time to perform deletion or garbage collection on your buckets, and leave the rules disabled at other times.
>
> Expiration – Specifies an expiration period for the objects that are subject to the rule, as a number of days from the object’s creation date.
>
> Id – Optional, gives a name to the rule.

This rule will then be evaluated daily and any expired objects will be removed.

See https://aws.amazon.com/blogs/aws/amazon-s3-object-expiration/ for a more in-depth explanation.

答案2

得分: 4

使用Golang SDK在S3中过期对象的一种方法是使用标签来标记您的上传,例如:

Tagging: aws.String("temp=true")

然后,转到S3存储桶管理控制台,并设置一个生命周期规则,针对该特定标签进行如下设置。

S3对象在使用Golang SDK时未过期

您可以在生命周期规则的创建过程中配置过期对象的时间范围。

英文:

One way to expire objects in S3 using Golang SDK is to tag your upload with something like

Tagging: aws.String("temp=true")

Then, Go to S3 Bucket Managment Console and Set a LifeCycle Rule targeting for that specific tag like this.

S3对象在使用Golang SDK时未过期

You can configure the time frame to expire the object during the creation of the Rule in LifeCycle.

答案3

得分: 0

你需要设置s3.PresignOptions.Expires,像这样:

func PreSignPutObject(cfg aws.Config, bucket, objectKey string) (string, error) {
	client := s3.NewFromConfig(cfg)
	psClient := s3.NewPresignClient(client)

	input := &s3.PutObjectInput{
		Bucket: &bucket,
		Key:    &objectKey,
	}
	resp, err := psClient.PresignPutObject(context.Background(), input, func(options *s3.PresignOptions){
		options.Expires = 3600 * time.Second
	})
	if err != nil {
		return "", err
	}

	return resp.URL, nil
}
英文:

you need to set s3.PresignOptions.Expires, like this:

func PreSignPutObject(cfg aws.Config, bucket, objectKey string) (string, error) {
	client := s3.NewFromConfig(cfg)
	psClient := s3.NewPresignClient(client)

	input := &s3.PutObjectInput{
		Bucket: &bucket,
		Key:    &objectKey,
	}
	resp, err := psClient.PresignPutObject(context.Background(), input, func(options *s3.PresignOptions){
		options.Expires = 3600 * time.Second
	})
	if err != nil {
		return "", err
	}

	return resp.URL, nil
}

huangapple
  • 本文由 发表于 2016年10月27日 02:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/40269901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定