Skip to content

Commit

Permalink
S3: Add Public ACL flag - default to private.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ami Mahloof authored and bep committed Jan 1, 2019
1 parent 48a7d5f commit ed74ea6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Pre-built binaries can be found [here](https://github.com/bep/s3deploy/releases/
```bash
go get -u -v github.com/bep/s3deploy
```

To install on MacOS using Homebrew:

```bash
Expand All @@ -45,6 +45,8 @@ Usage of s3deploy:
access key ID for AWS
-max-delete int
maximum number of files to delete per deploy (default 256)
-public-access
set public ACL on uploaded objects, defaults to private if not set.
-path string
optional bucket sub path
-quiet
Expand Down Expand Up @@ -104,8 +106,8 @@ routes:
Cache-Control: "max-age=630720000, no-transform, public"
gzip: false
- route: "^.+\\.(html|xml|json)$"
gzip: true
```
gzip: true
```
## Example IAM Policy
Expand Down Expand Up @@ -136,7 +138,7 @@ routes:
```
Replace <bucketname> with your own.
## CloudFront CDN Cache Invalidation
If you have configured CloudFront CDN in front of your S3 bucket, you can supply the `distribution-id` as a flag. This will make sure to invalidate the cache for the updated files after the deployment to S3. Note that the AWS user must have the needed access rights.
Expand Down
3 changes: 2 additions & 1 deletion lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Config struct {

NumberOfWorkers int
MaxDelete int

PublicReadACL bool
Verbose bool
Silent bool
Force bool
Expand Down Expand Up @@ -68,6 +68,7 @@ func flagsToConfig(f *flag.FlagSet) (*Config, error) {
f.StringVar(&cfg.CDNDistributionID, "distribution-id", "", "optional CDN distribution ID for cache invalidation")
f.StringVar(&cfg.ConfigFile, "config", ".s3deploy.yml", "optional config file")
f.IntVar(&cfg.MaxDelete, "max-delete", 256, "maximum number of files to delete per deploy")
f.BoolVar(&cfg.PublicReadACL, "public-access", false, "set public ACL on uploaded objects, defaults to private if not set.")
f.BoolVar(&cfg.Force, "force", false, "upload even if the etags match")
f.BoolVar(&cfg.Try, "try", false, "trial run, no remote updates")
f.BoolVar(&cfg.Verbose, "v", false, "enable verbose logging")
Expand Down
2 changes: 2 additions & 0 deletions lib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestFlagsToConfig(t *testing.T) {
"-key=mykey",
"-secret=mysecret",
"-max-delete=42",
"-public-access=true",
"-path=mypath",
"-quiet=true",
"-region=myregion",
Expand All @@ -39,6 +40,7 @@ func TestFlagsToConfig(t *testing.T) {
assert.Equal("mykey", cfg.AccessKey)
assert.Equal("mysecret", cfg.SecretKey)
assert.Equal(42, cfg.MaxDelete)
assert.Equal(true, cfg.PublicReadACL)
assert.Equal("mypath", cfg.BucketPath)
assert.Equal(true, cfg.Silent)
assert.Equal("mysource", cfg.SourcePath)
Expand Down
1 change: 1 addition & 0 deletions lib/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestDeploy(t *testing.T) {
RegionName: "eu-west-1",
ConfigFile: configFile,
MaxDelete: 300,
PublicReadACL: true,
Silent: true,
SourcePath: source,
baseStore: store,
Expand Down
12 changes: 9 additions & 3 deletions lib/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type s3Store struct {
bucket string
r routes
svc *s3.S3

publicReadACL bool
cfc *cloudFrontClient
}

Expand Down Expand Up @@ -59,7 +59,7 @@ func newRemoteStore(cfg Config, logger printer) (*s3Store, error) {
}
}

s = &s3Store{svc: s3.New(sess), cfc: cfc, bucket: cfg.BucketName, r: cfg.conf.Routes}
s = &s3Store{svc: s3.New(sess), cfc: cfc, publicReadACL: cfg.PublicReadACL, bucket: cfg.BucketName, r: cfg.conf.Routes}

return s, nil

Expand Down Expand Up @@ -93,11 +93,17 @@ func (s *s3Store) Put(ctx context.Context, f localFile, opts ...opOption) error
}
}

acl := aws.String("private")

if s.publicReadACL {
acl = aws.String("public-read")
}

_, err := s.svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(f.Key()),
Body: f.Content(),
ACL: aws.String("public-read"),
ACL: acl,
ContentLength: aws.Int64(f.Size()),
}, withHeaders)

Expand Down

0 comments on commit ed74ea6

Please sign in to comment.