- 最新
- 最多得票
- 最多評論
You can try these steps:
-
Create an IAM Role for the API Gateway:
- Create an IAM role that grants the necessary permissions for API Gateway to access the S3 bucket.
- Attach a policy that allows the
s3:PutObject
action on the desired S3 bucket.
-
Configure the API Gateway Integration Request:
- In the API Gateway console, create a new API or navigate to an existing one.
- Add a new resource with the path
/upload
. - Create a new method for the
/upload
resource, and selectPOST
as the HTTP method. - In the method setup, choose
Integration Type
asAWS
. - For
AWS Service
, selectS3
. - For
AWS Action
, selectPutObject
. - In the
Integration Request
section:- Integration URI: Set the Integration URI to the following format:
arn:aws:apigateway:{region}:s3:path/{bucket}/{object_key}
- Replace
{region}
with the AWS region where your S3 bucket is located. - Replace
{bucket}
with the name of your S3 bucket. {object_key}
will be the placeholder for the object key that you'll pass from the client.
- Replace
- Mapping Templates: Add a mapping template for the
application/json
content type. In the template, you can define the object key that will be used for thePutObject
action. For example:
This maps the{ "key": "$input.path('$.objectKey')" }
objectKey
property from the client's request body to thekey
parameter in thePutObject
action.
- Integration URI: Set the Integration URI to the following format:
-
Configure the API Gateway Method Request:
- In the
Method Request
section, add a newRequest Body
parameter with the nameobjectKey
(or any other name you prefer). - Set the
Model
toMethod Request
and theRequired
field totrue
.
- In the
-
- After configuring the integration, you need to deploy the API Gateway.
- In the API Gateway console, go to the
Stages
section and create a new stage (e.g.,dev
,prod
) or use an existing one. - Deploy the API to the selected stage.
Now, when a client sends a POST request to the /upload
resource, the API Gateway will forward the request to the S3 service, using the PutObject
action and the objectKey
value from the request body.
Here's an example of how the client can make a request to the API Gateway:
{ "objectKey": "example-file.txt" }
The API Gateway will then use the provided
objectKey
value to construct thePutObject
request and upload the file to the specified S3 bucket.
Hi,
There is a great knowledge article to help you upload files to S3 using API Gateway, I am sure, you would find an approach from that.
Hope this helps!
Thanks, Rama
Hi @Rama
That article uses
path override
approach creating resource like\{folder}\{item}
. This is mentioned in AWS documentation tutorial and in all the examples in repost and internet. But there is no single example using S3 for with action type asaction name = PutObject
. In documentation it is mentioned that it is possible but no example given.
相關內容
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 7 個月前
HI @Osvaldo. Thanks for your answer. In the documentation there is a reference to use different URI for action based S3 services.
arn:aws:apigateway:api-region:s3:action/GetBucket
https://docs.aws.amazon.com/apigateway/latest/developerguide/integration-request-basic-setup.html
Is it not possible to use this URI for PutObject ?
I think it is possible to use the URI. For the
PutObject
action, the integration request URI (action-based integration) in API Gateway would look like this:Since the bucket name and object key are not in the URI, you need to specify them in the integration request body, along with any other required parameters for the
PutObject
action. For example: