Skip to content

Demo http S3 generate presigned url - generate not only GET but also PUT (for http S3 upload demo) #1933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ In order to set these configurations manually, edit `demo_config.h` in `demos/ht
* Set `S3_PRESIGNED_GET_URL` to a S3 presigned URL with GET access.
* Set `S3_PRESIGNED_PUT_URL` to a S3 presigned URL with PUT access.

You can generate the presigned urls using [demos/http/common/src/presigned_urls_gen.py](demos/http/common/src/presigned_urls_gen.py). More info can be found [here](demos/http/common/src/README.md).
You can generate the presigned urls using [demos/http/common/src/presigned_urls_gen.py](demos/http/common/src/presigned_urls_gen.py) (more info can be found [here](demos/http/common/src/README.md)) or [demos/http/http_demo_s3_generate_presigned_url/README.md](demos/http/http_demo_s3_generate_presigned_url/README.md).

#### Configure S3 Download HTTP Demo using SigV4 Library:

Expand Down
5 changes: 3 additions & 2 deletions demos/http/http_demo_s3_generate_presigned_url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ Run the following command in the AWS CLI to create an IAM role with the precedin
```sh
aws iam create-role --role-name s3-access-role --assume-role-policy-document file://trustpolicyforiot.json
```
The following s3 access policy allows you to perform actions on S3. Put the following policy in a text document and save the document with the name `accesspolicyfors3.json`.
The following s3 access policy allows you to perform GET and PUT actions on S3. You can remove the "s3:PutObject" if only download is required (no upload e.g. http_demo_s3_upload). Put the following policy in a text document and save the document with the name `accesspolicyfors3.json`.
```
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": [
"s3:GetObject"
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::BUCKET_NAME/*"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ struct NetworkContext
*/
static bool printS3ObjectFilePresignedURL( const char * pHost,
size_t hostLen,
const char * pPath );
const char * pPath,
const bool is_get );

/**
* @brief CryptoInterface provided to SigV4 library for generating the hash digest.
Expand Down Expand Up @@ -261,7 +262,8 @@ static SigV4Parameters_t sigv4Params =

static bool printS3ObjectFilePresignedURL( const char * pHost,
size_t hostLen,
const char * pPath )
const char * pPath,
const bool is_get )
{
bool returnStatus = true;
HTTPStatus_t httpStatus = HTTPSuccess;
Expand Down Expand Up @@ -291,8 +293,16 @@ static bool printS3ObjectFilePresignedURL( const char * pHost,
/* Initialize the request object. */
requestInfo.pHost = pHost;
requestInfo.hostLen = hostLen;
requestInfo.pMethod = HTTP_METHOD_GET;
requestInfo.methodLen = sizeof( HTTP_METHOD_GET ) - 1;
if( is_get )
{
requestInfo.pMethod = HTTP_METHOD_GET;
requestInfo.methodLen = sizeof( HTTP_METHOD_GET ) - 1;
}
else
{
requestInfo.pMethod = HTTP_METHOD_PUT;
requestInfo.methodLen = sizeof( HTTP_METHOD_PUT ) - 1;
}
requestInfo.pPath = pPath;
requestInfo.pathLen = strlen( pPath );

Expand Down Expand Up @@ -523,9 +533,20 @@ int main( int argc,

if( returnStatus == EXIT_SUCCESS )
{
LogInfo( ( "HTTP_METHOD_GET:" ) );
ret = printS3ObjectFilePresignedURL( serverHost,
serverHostLength,
pPath );
pPath,
true/*is_get*/ );

if( ret )
{
LogInfo( ( "HTTP_METHOD_PUT:" ) );
ret = printS3ObjectFilePresignedURL( serverHost,
serverHostLength,
pPath,
false/*is_get*/ );
}

returnStatus = ( ret == true ) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Expand Down
Loading