Hey I got a question. I need to get the content of...
# general
d
Hey I got a question. I need to get the content of a file from S3 in pulumi, the file is a
.json
or a
.yaml
file, but when calling
object.body()
it shows undefined. I wonder is this the proper way to get the content or any other suggestions on how to get that? The code I have now:
Copy code
const latestFile =  aws.s3.getBucketObject({
        bucket: bucket,
        key: `${key}/pod-settings_latest.json`,
    });
    console.log(`latest file: ${latestFile.body}`);
which have the output as
undefined
.
w
I believe the
body
property is only populated when the
content-type
is
test/*
or
application/json
. See note at https://pulumi.io/reference/pkg/nodejs/pulumi/aws/s3/#getBucketObject.
d
I saw that.
so there's no way to get the file other then text and json?
p
I've had a few cases now where I was tempted to just use the aws sdk for stuff that I need to consume (vs create)
w
You could use the
aws
JS SDK directly.
p
no idea how that would play with plan/apply tho
d
Hmm, is there an example of how to do that?
w
By default, it would run during every plan and apply - which in fact
getBucketObject
will as well.
d
Basically we just have a bunch of configuration files in yaml format stored in S3, and we need to get these values in pulumi, as an input for other objects.
w
The most significant "tricky" think about using the AWS SDK directly is credentials - it will not automatically pick up the same credentials as you used to configure pulumi-aws - so you may need to do some additional work to make sure the SDK is configured with the same credentials.
@damp-pillow-67781 Yes - that sounds like a reasonable use case for using the AWS SDK.
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awssdk from "aws-sdk";

const bucket = new aws.s3.Bucket("my-bucket");
const obj = new aws.s3.BucketObject("o", {
    bucket: bucket,
    key: "foo.json",
    content: '{"foo": "bar"}',
    contentType: "application/json",
})

export const contents = pulumi.all([bucket.id, obj.key]).apply(async ([bucketId, key]) => {
    // const o = await aws.s3.getBucketObject({
    //     bucket: bucketId,
    //     key: key,
    // });
    const s3 = new awssdk.S3();
    const o = await s3.getObject({
        Bucket:  bucketId, 
        Key: key,
    }).promise();
    console.log(JSON.stringify(o));
    return (o.Body as Buffer).toString();
});
d
Oh cool, looking into that, thanks!