Hi guys, I’m trying create a folder into AWS S3 bu...
# aws
m
Hi guys, I’m trying create a folder into AWS S3 bucket but I do not have success yet, I’m using that code in Python:
Copy code
s3.BucketObject("obj", bucket=bucket.id, source=pulumi.AssetArchive({"folder": pulumi.FileArchive("./segment-logs")}))
Can someone help me in figure out it?
b
S3 doesn't have a concept of folders. You have to set the key to be something like
path/to/folder/file.txt
https://www.pulumi.com/docs/reference/pkg/aws/s3/bucketobject/#key_python
m
@brave-planet-10645 thank you for your feedback, sorry for my wrong terminology but I would like just offer a folder in the provisioning time when I’m creating the AWS S3 bucket, some solutions required that you create an AWS S3 and create a folder together, for example, https://segment.com/docs/connections/storage/catalog/amazon-s3/
Copy code
...
},
    "Action": "s3:PutObject",
	"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/segment-logs/*"
}
...
Can you help me in how create that object into my new AWS S3 bucket?
b
You can't create a folder in the bucket. You create the folder with the object. Let me just work out how to do this in python
👍 1
So what you'll need to do is loop through the folder containing the files you want to push to S3 and create bucketobjects out of them. So something like:
Copy code
directory=r'./segment-logs'
for entry in os.scandir(directory):
    bucket_object = s3.BucketObject(
        f'file-{entry.name}',
        source=pulumi.FileAsset(entry.path),
        bucket=bucket,
        key=f'segment-logs/{entry.name}'
    )
👍 1
note in the
key
I've added a prefix of
segment-logs/
prefix. That's your "folder"
👏 1
m
@brave-planet-10645 thank you so much for your help, I’ll try right now and give you feedback soon.
b
One thing to note... if you delete the files from your local
segement-logs
folder Pulumi will delete them from your S3 bucket
👍 1
m
@brave-planet-10645 Thank you so much again for your help. I tested and it works properly. I would like to suggest adding this example or replace that example here: https://github.com/pulumi/examples/tree/master/aws-py-s3-folder because didn’t work for me. Maybe it can help other Pulumi Python users.