Update go S3 folder examples to recursively upload files
* Update go S3 folder examples to recursively upload files
Replaces ioutil.ReadDir with filepath.Walk to upload files to S3 recursively
* Use relative path for bucket object key
MODIFY
aws-go-s3-folder-component/s3folder.go
aws-go-s3-folder-component/s3folder.go
package main
import (
- "io/ioutil"
+ "io/fs"
"mime"
"path"
"path/filepath"
@@ -36,17 +36,28 @@ func NewS3Folder(ctx *pulumi.Context, bucketName string, siteDir string, args *F
}
// For each file in the directory, create an S3 object stored in `siteBucket`
- files, err := ioutil.ReadDir(siteDir)
+ err = filepath.Walk(siteDir, func(name string, info fs.FileInfo, err error) error {
if err != nil {
- return nil, err
+ return err
+ }
+ if !info.IsDir() {
+ rel, err := filepath.Rel(siteDir, name)
+ if err != nil {
+ return err
}
- for _, item := range files {
- name := item.Name()
- s3.NewBucketObject(ctx, name, &s3.BucketObjectArgs{
+
+ if _, err := s3.NewBucketObject(ctx, rel, &s3.BucketObjectArgs{
Bucket: siteBucket.ID(), // reference to the s3.Bucket object
- Source: pulumi.NewFileAsset(filepath.Join(siteDir, name)), // use FileAsset to point to a file
+ Source: pulumi.NewFileAsset(name), // use FileAsset to point to a file
ContentType: pulumi.String(mime.TypeByExtension(path.Ext(name))), // set the MIME type of the file
- }, pulumi.Parent(&resource))
+ }, pulumi.Parent(&resource)); err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+ if err != nil {
+ return err
}
// Set the access policy for the bucket so all objects are readable.

RENAME
aws-go-s3-folder-component/www/favicon.png
aws-go-s3-folder-component/www/icon/favicon.png
MODIFY
aws-go-s3-folder/main.go
aws-go-s3-folder/main.go
package main
import (
- "io/ioutil"
+ "io/fs"
"mime"
"path"
"path/filepath"
@@ -25,20 +25,29 @@ func main() {
siteDir := "www" // directory for content files
// For each file in the directory, create an S3 object stored in `siteBucket`
- files, err := ioutil.ReadDir(siteDir)
+ err = filepath.Walk(siteDir, func(name string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
- for _, item := range files {
- name := item.Name()
- if _, err := s3.NewBucketObject(ctx, name, &s3.BucketObjectArgs{
+ if !info.IsDir() {
+ rel, err := filepath.Rel(siteDir, name)
+ if err != nil {
+ return err
+ }
+
+ if _, err := s3.NewBucketObject(ctx, rel, &s3.BucketObjectArgs{
Bucket: siteBucket.ID(), // reference to the s3.Bucket object
- Source: pulumi.NewFileAsset(filepath.Join(siteDir, name)), // use FileAsset to point to a file
+ Source: pulumi.NewFileAsset(name), // use FileAsset to point to a file
ContentType: pulumi.String(mime.TypeByExtension(path.Ext(name))), // set the MIME type of the file
}); err != nil {
return err
}
}
+ return nil
+ })
+ if err != nil {
+ return err
+ }
// Set the access policy for the bucket so all objects are readable.
if _, err := s3.NewBucketPolicy(ctx, "bucketPolicy", &s3.BucketPolicyArgs{

RENAME
aws-go-s3-folder/www/favicon.png
aws-go-s3-folder/www/icon/favicon.png
Update go S3 folder examples to recursively upload files (#1159)

* Update go S3 folder examples to recursively upload files

Replaces ioutil.ReadDir with filepath.Walk to upload files to S3 recursively

* Use relative path for bucket object key