so if it happened in aws lambda how can I upload t...
# help
a
so if it happened in aws lambda how can I upload the file directly to s3 without creating a file
r
Simplest way is usually to create an S3 presigned URL that you can post the file to. BTW, I don’t think you need
return await new Promise
return new Promise
should be sufficient
a
aa just stupid mistake
so, if I will have presigned s3 url how can I sent pipe stream to body for presigned url upload?
r
I believe you can stream to the URL. There are some examples here, although none in JavaScript unfortunately, https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html
a
ok, thanks, I think I can read other languages
r
Cool
a
can I create presigned url using sst??
r
You’d need to create it dynamically when needed in the lambda
they expire after a period of time
a
probably the question is incorrect
how can i get me s3 object in sst?
Copy code
const s3 = new AWS.S3({
  accessKeyId: /* Bucket owner access key id */,
  secretAccessKey: /* Bucket owner secret */,
  sessionToken: `session-${cuid()}`
})
r
I guess it depends on your use case but you can have full access to the SDK should you need it
a
so I beleive sst have my accessKeyId and secretAccessKey somewhere
r
Is it that you’re not comfortable using key and secret? Maybe a question for the SST experts then.
a
yea, anyway I will try to use it as usual, but yea, I beleive sst have it somewhere, because the rest of the code doesn’t need it
f
@Artem Kalantai r u trying to get the S3 object in cdk code or lambda code?
a
cdk
actually i already did what i need, but i believe it should be more beautiful way
Copy code
const downloadFromGoogleDrive = async (authToken, file) => {
  const credentials = {
    accessKeyId: '',
    secretAccessKey : ''
  };
  AWS.config.update({credentials: credentials, region: 'us-east-1'});
  const s3 = new AWS.S3();

  return fetch(`<https://www.googleapis.com/drive/v2/files/${file.id}?alt=media&source=downloadUrl>`, {
    method: 'GET',
    headers: {
      "Authorization": 'Bearer ' + authToken
    }
  }).then((response) => {
      if (response.ok) {
        return response;
      }
      return Promise.reject(new Error(
        `Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
    })
    .then(response => response.buffer())
    .then(buffer => (
      s3.putObject({
        Bucket: 'fanpoint-content',
        Key: 'testUser/image.jpg',
        ContentType: 'image/jpeg',
        Expires: 100,
        Body: buffer,
      }).promise()
    ));
}
this i did
but should be a better way, do you think?
f
are you trying to use different IAM credentials as the ones you are using to run the SST cli command?
Why do you have to change the credentials?
Copy code
const credentials = {
    accessKeyId: '',
    secretAccessKey : ''
  };
  AWS.config.update({credentials: credentials, region: 'us-east-1'});
a
aaa hmmmm
probably because I’m still not good enough in it
f
lol no i’m just curious… b/c by default CDK looks for the
default
AWS credentials you configured in your termimal
a
thanks for advice 🙂
f
so by default all AWS SDK calls in your CDK code would use that IAM credential
a
i see, yea
what about the rest of the code? looks good or can be better?
so I’m trying to download from gdrive and upload to s3
now it works fine
so, I’m doing it using SDK, but can I do it using CDK?
or maybe i don’t need
f
You would still need to do the http fetch here, and S3 Deployment construct will do the uploading to S3 for you.
r
Personally I’d rewrite using async/await as I find it much easier to read - something like this
Copy code
const downloadFromGoogleDrive = async (authToken, file) => {
  const credentials = {
    accessKeyId: '',
    secretAccessKey : ''
  };
  AWS.config.update({credentials: credentials, region: 'us-east-1'});
  const s3 = new AWS.S3();
  const response = await fetch(`<https://www.googleapis.com/drive/v2/files/${file.id}?alt=media&source=downloadUrl>`, {
    method: 'GET',
    headers: {
      "Authorization": 'Bearer ' + authToken
    }
  })
  if (!response.ok) {
    throw Error(`Failed to fetch ${response.url}: ${response.status} ${response.statusText}`)
  }
  const buffer = response.buffer();
  return s3.putObject({
    Bucket: 'fanpoint-content',
    Key: 'testUser/image.jpg',
    ContentType: 'image/jpeg',
    Expires: 100,
    Body: buffer,
  }).promise()  
}
a
guys, thanks you both
I’ll read more about the cdk s3 construct and yes I’ll using async of course