Skip to content

Commit

Permalink
Make it possible to override content-type and content-encoding from C…
Browse files Browse the repository at this point in the history
…LI arguments (peak#467)
  • Loading branch information
nattofriends committed Jul 20, 2022
1 parent 717bf5b commit 38b0fdf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
## not released yet

#### Features
- Added `--content-type` and `--content-encoding` flags to `cp` command. ([#264](https://github.com/peak/s5cmd/issues/264))
- Added `--profile` flag to allow users to specify a [named profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html). ([#353](https://github.com/peak/s5cmd/issues/353))
- Added `--credentials-file` flag to allow users to specify path for the AWS credentials file instead of using the [default location](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-where).

#### Improvements
- Disable AWS SDK logger if log level is not "trace"

#### Bugfixes
- Fixed a bug where (`--stat`) prints unnecessarily when used with help and version commands ([#452](https://github.com/peak/s5cmd/issues/452))
- Changed cp error message to be more precise. "given object not found" error message now will also include absolute path of the file. ([#463](https://github.com/peak/s5cmd/pull/463))

#### Improvements
- Disable AWS SDK logger if log level is not "trace"

## v2.0.0 - 4 Jul 2022

#### Breaking changes
Expand Down
33 changes: 32 additions & 1 deletion command/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Examples:
20. Download an S3 object from a requester pays bucket
> s5cmd --request-payer=requester {{.HelpName}} s3://bucket/prefix/object.gz .
21. Upload a file to S3 with a content-type and content-encoding header
> s5cmd --content-type "text/css" --content-encoding "br" myfile.css.br s3://bucket/
`

func NewSharedFlags() []cli.Flag {
Expand Down Expand Up @@ -166,6 +169,14 @@ func NewSharedFlags() []cli.Flag {
Name: "raw",
Usage: "disable the wildcard operations, useful with filenames that contains glob characters",
},
&cli.StringFlag{
Name: "content-type",
Usage: "set content type for target: defines content type header for object, e.g. --content-type text/plain",
},
&cli.StringFlag{
Name: "content-encoding",
Usage: "set content encoding for target: defines content encoding header for object, e.g. --content-encoding gzip",
},
}
}

Expand Down Expand Up @@ -244,6 +255,8 @@ type Copy struct {
raw bool
cacheControl string
expires string
contentType string
contentEncoding string

// region settings
srcRegion string
Expand Down Expand Up @@ -281,6 +294,8 @@ func NewCopy(c *cli.Context, deleteSource bool) Copy {
raw: c.Bool("raw"),
cacheControl: c.String("cache-control"),
expires: c.String("expires"),
contentType: c.String("content-type"),
contentEncoding: c.String("content-encoding"),
// region settings
srcRegion: c.String("source-region"),
dstRegion: c.String("destination-region"),
Expand Down Expand Up @@ -550,14 +565,23 @@ func (c Copy) doUpload(ctx context.Context, srcurl *url.URL, dsturl *url.URL) er
}

metadata := storage.NewMetadata().
SetContentType(guessContentType(file)).
SetStorageClass(string(c.storageClass)).
SetSSE(c.encryptionMethod).
SetSSEKeyID(c.encryptionKeyID).
SetACL(c.acl).
SetCacheControl(c.cacheControl).
SetExpires(c.expires)

if c.contentType != "" {
metadata.SetContentType(c.contentType)
} else {
metadata.SetContentType(guessContentType(file))
}

if c.contentEncoding != "" {
metadata.SetContentEncoding(c.contentEncoding)
}

err = dstClient.Put(ctx, file, dsturl, metadata, c.concurrency, c.partSize)
if err != nil {
return err
Expand Down Expand Up @@ -606,6 +630,13 @@ func (c Copy) doCopy(ctx context.Context, srcurl, dsturl *url.URL) error {
SetCacheControl(c.cacheControl).
SetExpires(c.expires)

if c.contentType != "" {
metadata.SetContentType(c.contentType)
}
if c.contentEncoding != "" {
metadata.SetContentEncoding(c.contentEncoding)
}

err = c.shouldOverride(ctx, srcurl, dsturl)
if err != nil {
if errorpkg.IsWarning(err) {
Expand Down
5 changes: 5 additions & 0 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,11 @@ func (s *S3) Put(
}
}

contentEncoding := metadata.ContentEncoding()
if contentEncoding != "" {
input.ContentEncoding = aws.String(contentEncoding)
}

_, err := s.uploader.UploadWithContext(ctx, input, func(u *s3manager.Uploader) {
u.PartSize = partSize
u.Concurrency = concurrency
Expand Down
9 changes: 9 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,12 @@ func (m Metadata) SetSSEKeyID(kid string) Metadata {
m["EncryptionKeyID"] = kid
return m
}

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

func (m Metadata) SetContentEncoding(contentEncoding string) Metadata {
m["ContentEncoding"] = contentEncoding
return m
}

0 comments on commit 38b0fdf

Please sign in to comment.