Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cache-control and expires options #318

Merged
merged 7 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions command/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,18 @@ Examples:

13. Perform KMS-SSE of the object(s) at the destination using customer managed Customer Master Key (CMK) key id
> s5cmd {{.HelpName}} --sse aws:kms --sse-kms-key-id <your-kms-key-id> s3://bucket/object s3://target-bucket/prefix/object

14. Force transfer of GLACIER objects with a prefix whether they are restored or not
> s5cmd {{.HelpName}} --force-glacier-transfer s3://bucket/prefix/* target-directory/

15. Upload a file to S3 bucket with public read s3 acl
> s5cmd {{.HelpName}} --acl "public-read" myfile.gz s3://bucket/

16. Upload a file to S3 bucket with expires header
> s5cmd {{.HelpName}} --expires "2024-10-01T20:30:00Z" myfile.gz s3://bucket/

17. Upload a file to S3 bucket with cache-control header
> s5cmd {{.HelpName}} --cache-control "public, max-age=345600" myfile.gz s3://bucket/
`

var copyCommandFlags = []cli.Flag{
Expand Down Expand Up @@ -132,7 +141,15 @@ var copyCommandFlags = []cli.Flag{
},
&cli.StringFlag{
Name: "acl",
Usage: "set acl for target: defines granted accesses and their types on different accounts/groups",
Usage: "set acl for target: defines granted accesses and their types on different accounts/groups, e.g. cp --acl 'public-read'",
},
&cli.StringFlag{
Name: "cache-control",
Usage: "set cache control for target: defines cache control header for object, e.g. cp --cache-control 'public, max-age=345600'",
},
&cli.StringFlag{
Name: "expires",
Usage: "set expires for target (uses RFC3339 format): defines expires header for object, e.g. cp --expires '2024-10-01T20:30:00Z'",
},
&cli.BoolFlag{
Name: "force-glacier-transfer",
Expand Down Expand Up @@ -183,6 +200,8 @@ var copyCommand = &cli.Command{
encryptionKeyID: c.String("sse-kms-key-id"),
acl: c.String("acl"),
forceGlacierTransfer: c.Bool("force-glacier-transfer"),
cacheControl: c.String("cache-control"),
expires: c.String("expires"),
// region settings
srcRegion: c.String("source-region"),
dstRegion: c.String("destination-region"),
Expand Down Expand Up @@ -212,6 +231,9 @@ type Copy struct {
encryptionKeyID string
acl string
forceGlacierTransfer bool
cacheControl string
expires string


// region settings
srcRegion string
Expand Down Expand Up @@ -472,7 +494,9 @@ func (c Copy) doUpload(ctx context.Context, srcurl *url.URL, dsturl *url.URL) er
SetStorageClass(string(c.storageClass)).
SetSSE(c.encryptionMethod).
SetSSEKeyID(c.encryptionKeyID).
SetACL(c.acl)
SetACL(c.acl).
SetCacheControl(c.cacheControl).
SetExpires(c.expires)

err = dstClient.Put(ctx, file, dsturl, metadata, c.concurrency, c.partSize)
if err != nil {
Expand Down Expand Up @@ -518,7 +542,9 @@ func (c Copy) doCopy(ctx context.Context, srcurl, dsturl *url.URL) error {
SetStorageClass(string(c.storageClass)).
SetSSE(c.encryptionMethod).
SetSSEKeyID(c.encryptionKeyID).
SetACL(c.acl)
SetACL(c.acl).
SetCacheControl(c.cacheControl).
SetExpires(c.expires)

err = c.shouldOverride(ctx, srcurl, dsturl)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions command/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ var moveCommand = &cli.Command{
encryptionMethod: c.String("sse"),
encryptionKeyID: c.String("sse-kms-key-id"),
acl: c.String("acl"),
cacheControl: c.String("cache-control"),
expires: c.String("expires"),


storageOpts: NewStorageOpts(c),
}
Expand Down
28 changes: 28 additions & 0 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,20 @@ func (s *S3) Copy(ctx context.Context, from, to *url.URL, metadata Metadata) err
input.ACL = aws.String(acl)
}

cacheControl := metadata.CacheControl()
if cacheControl != "" {
input.CacheControl = aws.String(cacheControl)
}

expires := metadata.Expires()
if expires != "" {
t, err := time.Parse(time.RFC3339, expires)
if err != nil {
return err
}
input.Expires = aws.Time(t)
}

_, err := s.api.CopyObject(input)
return err
}
Expand Down Expand Up @@ -492,6 +506,20 @@ func (s *S3) Put(
input.ACL = aws.String(acl)
}

cacheControl := metadata.CacheControl()
if cacheControl != "" {
input.CacheControl = aws.String(cacheControl)
}

expires := metadata.Expires()
if expires != "" {
t, err := time.Parse(time.RFC3339, expires)
if err != nil {
return err
}
input.Expires = aws.Time(t)
}

sseEncryption := metadata.SSE()
if sseEncryption != "" {
input.ServerSideEncryption = aws.String(sseEncryption)
Expand Down
18 changes: 18 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ func (m Metadata) SetACL(acl string) Metadata {
return m
}

func (m Metadata) CacheControl() string {
return m["CacheControl"]
}

func (m Metadata) SetCacheControl(cacheControl string) Metadata {
m["CacheControl"] = cacheControl
return m
}

func (m Metadata) Expires() string {
return m["Expires"]
}

func (m Metadata) SetExpires(expires string) Metadata {
m["Expires"] = expires
return m
}

func (m Metadata) StorageClass() string {
return m["StorageClass"]
}
Expand Down