https://pulumi.com logo
Title
e

elegant-crayon-4967

04/28/2021, 9:14 PM
trying to upload a list of files to S3, then save the arns of those objects to a list that I can pass later in my program. Anyone know of an elegant way to do this?
b

billowy-army-68599

04/28/2021, 9:16 PM
hang on, let me throw an example together
e

elegant-crayon-4967

04/28/2021, 9:17 PM
you would be a hero in my eyes!
FYI..here is my code that I tried but not working
const objectIds = [];
fs.readdir('components', (err, files) => {
	if (err) {
		return console.log('Unable to scan directory:' + err);
	}
	files.forEach((file) => {
		const fileName = file.split(".");
		const filePath = require("path").join('components', file)
		// Upload File to S3
		const componentObject = new aws.s3.BucketObject(`${config.name}-${fileName[0]}-object`, {
			key: file,
			source: new pulumi.asset.FileAsset(filePath),
			bucket: bucket.id
		});

		objectIds.push({
			objectId: componentObject.id
	});
});
b

billowy-army-68599

04/28/2021, 9:27 PM
what error are you getting?
e

elegant-crayon-4967

04/28/2021, 9:28 PM
no error, the array is just empty
so I’m sure I’m not handling promises properly (which I’ve always struggled to do)
b

billowy-army-68599

04/28/2021, 9:42 PM
I thought I had done this before but apparently not with the ids, @white-balloon-205 don't suppose you could help here?
oh wait, yes I have, incoming!
e

elegant-crayon-4967

04/28/2021, 9:44 PM
🕺
b

billowy-army-68599

04/28/2021, 9:45 PM
this works for me: https://github.com/jaxxstorm/pulumi-examples/blob/master/typescript/aws/s3/index.ts you need to make sure the
objectIds
type is correct
I've used a map here, but a for loop should work the same
e

elegant-crayon-4967

04/28/2021, 9:47 PM
going to give it a shot, thanks!
I get this as my output of the ID
OutputImpl {
      __pulumiOutput: true,
      resources: [Function (anonymous)],
      allResources: [Function (anonymous)],
      isKnown: Promise { <pending> },
      isSecret: Promise { <pending> },
      promise: [Function (anonymous)],
      toString: [Function (anonymous)],
      toJSON: [Function (anonymous)]
    }
b

billowy-army-68599

04/29/2021, 4:40 PM
hey, did you get this figured out?
e

elegant-crayon-4967

04/29/2021, 5:10 PM
Not really. I can’t out how to reference the array later in my code. It’s always empty or undefined
export let objectIds: pulumi.Output<string>[] = []

const objects = ["installSsm.yml"].map((name) => {
	let obj = new aws.s3.BucketObject(name, {
		bucket: bucket.id,
		source: new pulumi.asset.FileAsset(`./components/${name}`),
	})
	objectIds.push(obj.id)
	//console.log(obj.id)
	
})

console.log(objectIds)
I just don’t know how to print that objectIds promise to show the real contents instead of the
OutputImpl {….
and then assign that to a parameter later
b

billowy-army-68599

04/29/2021, 5:20 PM
@elegant-crayon-4967 you can't print it outside an apply,
console.log
won't work because it's asynchronously resolved. With the export, did it show you the values?
e

elegant-crayon-4967

04/29/2021, 5:23 PM
I can see the values when I’m inside arrow function
so how would I use the
.apply
agains the array?
b

billowy-army-68599

04/29/2021, 5:29 PM
what do you actually want to do with the values? it'll help figuring out the best way to use them
e

elegant-crayon-4967

04/29/2021, 5:31 PM
I’ll shoot you a DM