Unlike most languages with interpolated strings, Python's formatted string literals do not allow the nesting of quotes. For example, this expression is not legal Python: f"Foo {"bar"} baz" If an interpolation requires quotes, those quotes nust differ from the quotes used by the enclosing literal. We can fix the previous example by rewriting it with single quotes: f"Foo {'bar'} baz" However, this presents a problem if there are more than two levels of nesting, as Python only has two kinds of quotes (four if the outermost string uses """ or '''): in this case, the expression becomes unspellable, and must be assigned to a local that is then used in place of the original expression. So this: f"Foo {bar[f'index {baz["qux"]}']} zed" becomes this: index = "qux" f"Foo {bar[f'index {baz[index]}']}" To put it bluntly, Python code generation reqiures register allocation, but for quotes. These changes implement exactly that. These changes also include a fix for traversals that access values that are dictionaries rather than objects, and must use indexers rather than attributes.
33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
import pulumi
|
|
import json
|
|
import os
|
|
import pulumi_aws as aws
|
|
|
|
# Create a bucket and expose a website index document
|
|
site_bucket = aws.s3.Bucket("siteBucket", website={
|
|
"indexDocument": "index.html",
|
|
})
|
|
site_dir = "www"
|
|
# For each file in the directory, create an S3 object stored in `siteBucket`
|
|
files = []
|
|
for range in [{"key": k, "value": v} for [k, v] in enumerate(os.listdir(site_dir))]:
|
|
files.append(aws.s3.BucketObject(f"files-{range['key']}",
|
|
bucket=site_bucket.id,
|
|
key=range["value"],
|
|
source=pulumi.FileAsset(f"{site_dir}/{range['value']}"),
|
|
content_type=(lambda: raise Exception("FunctionCallExpression: mimeType (aws-s3-folder.pp:19,16-37)"))()))
|
|
# set the MIME type of the file
|
|
# Set the access policy for the bucket so all objects are readable
|
|
bucket_policy = aws.s3.BucketPolicy("bucketPolicy",
|
|
bucket=site_bucket.id,
|
|
policy=site_bucket.id.apply(lambda id: json.dumps({
|
|
"Version": "2012-10-17",
|
|
"Statement": [{
|
|
"Effect": "Allow",
|
|
"Principal": "*",
|
|
"Action": ["s3:GetObject"],
|
|
"Resource": [f"arn:aws:s3:::{id}/*"],
|
|
}],
|
|
})))
|
|
pulumi.export("bucketName", site_bucket.bucket)
|
|
pulumi.export("websiteUrl", site_bucket.website_endpoint)
|