upload binary from api gateway to S3 bucket - amazon-web-services

i was trying to create a rest api which can take zip file as input (PUT requst) and store that on S3.
I'm following the tutorial on http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-content-encodings-examples-image-s3.html
I'm getting 500 error ad the cloudwatch logs are as follows
Verifying Usage Plan for request:
c2140431-1a10-11e7-9f32-0df3853848fe. API Key: API Stage:
xjjd186a30/rd
API Key authorized because method 'PUT /s3' does not require API Key.
Request will not contribute to throttle or quota limits
Usage Plan check succeeded for API Key and API Stage xjjd186a30/rd
Starting execution for request: c2140431-1a10-11e7-9f32-0df3853848fe
HTTP Method: PUT, Resource Path: /s3
Successfully completed execution
Method completed with status: 500
when i try the api from post man i get
AccessDeniedAccess
DeniedF55D45C185A5BF11HXopfmxAxGNvmdi7PRp4c1j/wPYmGVTrkKbGXfZwofLOn7TRBPs3uFjer/2UCIktynKtGeNU1Xw=
my roles i have given AmazonS3FullAccess permission and have assigned the role to api gateway settings and the integration request.
can anyone help please

It looks like you are attempting to put to the bucket named rest.
Is that the correct bucket?
This documentation will probably be a little more helpful for you:
Integrating API with AWS S3
In the example in used in the documentation, a bucket and object are provided in the path override for the PUT item method. These are mapped from the path params folder and item.
Here is a helpful screenshot:

If you want to upload the binary files like mp3,audio, documents etc..., you can add an entry with value multipart/form-data in the Binary support in AWS API Gateway settings and post/put the binary file using the header Content-Type = multipart/form-data from postman or api client. It should work..!!
api gateway-binary support image

Related

AWS serverless url not found

I published an AWS Lambda NET solution that responded with an AWS Serverless URL:
https://je7p92w69l.execute-api.us-east-2.amazonaws.com/Prod
When I got to this link it is 404.
Check the root path for your API: your API's URL is https://je7p92w69l.execute-api.us-east-2.amazonaws.com/Prod which seems to be correct. However, haven't you mapped any API? Let's say you are creating a /books API, then you should invoke your API like https://je7p92w69l.execute-api.us-east-2.amazonaws.com/Prod/books
If you're getting 404 it just means that API Gateway is responding properly but this URI on your API does not exist. Just figure out the path you created and it will work.
On the left-hand side of your API you should see your resources, and, inside it, your URIs.

Getting error while invoking API using AWS Lambda. (AWS Lambda + AWS API Gateway+ Postman)

I get an error while invoking the AWS SageMaker endpoint API from a Lambda function. When I call this using Postman, I am getting an error like:
{
"errorMessage": "module initialization error"
}
Just to make it clear, you can't call SageMaker endpoints directly using PostMan (even if it is, it would not be straightforward).
You may need to use AWS SDK (i.e. boto) for that.
Ref : https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/
What I would suggest is to create a small HTTP server with Flask and use the AWS SDK (Boto) to call the endpoint. Then you can call your Flask endpoint using PostMan.
We recommend using AWS SDK to invoke your endpoint. AWS SDK clients handle the serialization for you as well as request signing, etc. It would be really hard to get it right manually with postman.
We have the SDK client available in many languages, including Java, Python, JS, etc.
https://docs.aws.amazon.com/sagemaker/latest/dg/API_runtime_InvokeEndpoint.html#API_runtime_InvokeEndpoint_SeeAlso
Next time please include more details in your question. eg. POST request data, Headers etc.
Anyways, to help you out in calling Sagemaker endpoint using Postman -
In 'Authorization' tab, select type as 'AWS Signature'.
Enter your Access and Secret key of the IAM user which has permission to Sagemaker resources.
Enter the AWS region. eg.us-east-1
Enter 'Service Name' as 'sagemaker'
Select the right content type. Some ML algorithms only accept 'text/csv'.
Select request type as 'POST'
Enter the Sagemaker Invocation url. eg:'https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/xgboost-xxxx-xx-xx-xx-xx-xx-xxx/invocations'
Try it out and let me know if you have any issues.
Here is how your Postman should look -

Upload file to s3 with custom response to client

I am trying to upload a file to s3 and then have lambda generate id, date.
I then want to return this data back to the client.
I want to avoid generating id and date on the client for security reasons.
Currently, I am trying to use API Gateway which invokes a lambda to upload into s3. However, I am having problems setting this up. I know that this is not a preferred method.
Is there another way to do this without writing my own web server. (I would like to use lambda).
If not, how can I configure my API Gateway method to support file upload to lambda?
You have a couple of options here:
Use API Gateway as an AWS Service Proxy to S3
Use API Gateway to invoke a Lambda function, which uses the AWS SDK to upload to S3
In either case, you will need to base64 encode the file content before calling API Gateway, and POST it in the request body.
We don't currently have any documentation on this exact use case but I would refer you to the S3 API and AWS SDK docs for more information. If you have any specific questions we'd be glad to help.
Thanks,
Ryan

CORS error with listBuckets in AWS JS SDK

I am trying to listBuckets associated with an authorized user in frontend using AWS JS SDK.
listBuckets API documentation:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listBuckets-property
And, listBucket request failed with the following error message:
https://s3-us-west-2.amazonaws.com/. Response to preflight request
doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin
'http://palombpramalis.local:8888' is therefore not allowed access.
The response had HTTP status code 403.
How to configure CORS for https://s3-us-west-2.amazonaws.com/?
AWS documentations talks about configuring CORS for specific bucket only
http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html.
But this request is for listing all buckets for an authenticated user.
S3 regional endpoints such as s3-region.amazonaws.com do not support CORS. CORS is only supported on buckets (after you've enabled it). So you cannot call listBuckets. It would be great if AWS enabled this, but there may be compelling reasons not to.
You may be able to work around this, if needed, by hard-coding bucket names in your web client (not ideal), or by maintaining a list of buckets in a readable JSON file stored in S3. Personally, I'd prefer the latter and would try to maintain the file using AWS Lambda. Or you could ask the user to supply the bucket name, of course, but they typically will not know it.

How to upload file to lambda function or API Gateway?

I'm trying to upload a file from iOS to AWS API Gateway and pass it through to a Lambda function, How can I implement this scenario?
I can use multipart/form-data to upload to AWS API Gateway but how make input Model support binary data?
[Edit1] moved from answer by Spektre
Thanks For response, after a little of reading I figure out that's no way to upload file to lambda (and it's not logical because it's event based) and the only valid use case to upload to S3 and make S3 notify lambda.
I'd highly recommend using direct S3 upload using one of the AWS SDKs. AWS Lambda is best suited for processing events, not content transfers like uploads. You can check its billing and limits to make a more informed decision on if it's really something you're looking for.
API Gateway has added support for an S3 Proxy. This allows you to expose file uploading directly to S3.
http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html
If want to upload file through lambda, one way is to open your AWS API Gateway console.
Go to
"API" -> {YourAPI} -> "Settings"
There you will find "Binary Media Types" section.
Add following media type:
multipart/form-data
Save your changes.
Then Go to "Resources" -> "proxy method"(eg. "ANY") -> "Method Request" -> "HTTP Request Headers" and add following headers "Content-Type", "Accept".
Finally deploy your api.
For more info visit: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html