Skip to content

Instantly share code, notes, and snippets.

@cbschuld
Created September 13, 2019 21:51
Show Gist options
  • Save cbschuld/f7efde2f3735248112577f7d41deadf6 to your computer and use it in GitHub Desktop.
Save cbschuld/f7efde2f3735248112577f7d41deadf6 to your computer and use it in GitHub Desktop.
Uploading Files to S3 using a PreSignedURL (using curl as well)

Uploading to a pre-signed URL in AWS

A few things you need to make sure you have done:

  • Need to make sure the user creating the presigned key has the ability to call PutObject on the target bucket
  • Need to make sure the bucket as the correct CORS on it

Bucket IAM Roles

In my use case I am using "serverless.com" - in serverless, it would look like this

provider:
  name: aws
  stage: ${opt:stage, 'dev'}
  region: RRRR
  runtime: nodejs10.x
  profile: XXXX
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
        - s3:GetObject
      Resource:
        - "arn:aws:s3:::${self:custom.buckets.${self:custom.stage}}/*"
    - Effect: Allow
      Action:
        - s3:ListBucket
      Resource:
        - "arn:aws:s3:::${self:custom.buckets.${self:custom.stage}}*"

CORS on the bucket(s)

BUCKETNAME=mybucket
aws --profile=XXXX s3api put-bucket-cors --bucket $BUCKETNAME --cors-configuration '{"CORSRules": [{"AllowedOrigins": ["*"], "AllowedMethods": ["PUT"], "AllowedHeaders": ["*"]}]}'

Testing with CURL

After you have the presigned URL you can upload to test via curl:

URL=[PRE_SIGNED_URL]
curl -X PUT -H "Content-Type: image/jpeg" -T picture.jpg $URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment