### Add and Get S3 Bucket Website Configuration (.NET) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/EnableWebsiteHosting This C# code example demonstrates how to use the AWS SDK for .NET to add and retrieve a website configuration for an Amazon S3 bucket. It specifies the index and error document names. Ensure the index and error documents exist in the bucket. For setup details, refer to the AWS SDK for .NET Developer Guide. ```csharp using Amazon; using Amazon.S3; using Amazon.S3.Model; using System; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class WebsiteConfigTest { private const string bucketName = "*** bucket name ***"; private const string indexDocumentSuffix = "*** index object key ***"; // For example, index.html. private const string errorDocument = "*** error object key ***"; // For example, error.html. // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); AddWebsiteConfigurationAsync(bucketName, indexDocumentSuffix, errorDocument).Wait(); } static async Task AddWebsiteConfigurationAsync(string bucketName, string indexDocumentSuffix, string errorDocument) { try { // 1. Put the website configuration. PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument } }; PutBucketWebsiteResponse response = await client.PutBucketWebsiteAsync(putRequest); // 2. Get the website configuration. GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest() { BucketName = bucketName }; GetBucketWebsiteResponse getResponse = await client.GetBucketWebsiteAsync(getRequest); Console.WriteLine("Index document: {0}", getResponse.WebsiteConfiguration.IndexDocumentSuffix); Console.WriteLine("Error document: {0}", getResponse.WebsiteConfiguration.ErrorDocument); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } } } ``` -------------------------------- ### Verify AWS CLI Configuration Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GettingStartedS3CLI This command verifies that your AWS Command Line Interface (AWS CLI) is installed and configured correctly by retrieving information about your AWS identity. It's a prerequisite before interacting with AWS services like Amazon S3. ```bash aws sts get-caller-identity ``` -------------------------------- ### AWS CLI - Create Bucket with Tags Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-create-tag Example commands using the AWS CLI to create a directory bucket with tags. This includes instructions for installation and placeholder replacement. ```APIDOC ## AWS CLI Command: create-bucket ### Description Creates a new directory bucket in Amazon S3 with specified tags using the AWS Command Line Interface. ### Method AWS CLI Command ### Endpoint Not applicable (CLI command) ### Parameters - **--bucket** (string) - Required - The name of the bucket to create. Must follow the naming convention: `bucket-base-name--zone-id--x-s3`. - **--create-bucket-configuration** (string) - Required - Configuration for the bucket. Example format: `"Location={Type=AvailabilityZone,Name=zone-id},Bucket={DataRedundancy=SingleAvailabilityZone,Type=Directory}"`. - **--tags** (string) - Optional - Specifies the tags to apply to the bucket. Example format: `"Key=mykey1,Value=myvalue1 Key=mykey2,Value=myvalue2"`. ### Request Example ```bash aws s3api create-bucket \ --bucket bucket-base-name--zone-id--x-s3 \ --create-bucket-configuration "Location={Type=AvailabilityZone,Name=zone-id},Bucket={DataRedundancy=SingleAvailabilityZone,Type=Directory}" \ --tags "Key=mykey1,Value=myvalue1 Key=mykey2,Value=myvalue2" ``` ### Response #### Success Response (200) - **Location** (string) - The URL of the created bucket. #### Response Example ```json { "Location": "http://bucket--use1-az4--x-s3.s3express-use1-az4.us-east-1.amazonaws.com/" } ``` ``` -------------------------------- ### Create a Text File for Upload (Bash) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GettingStartedS3CLI This command uses `echo` to create a simple text file named `example.txt` with the content 'Hello, Amazon S3!'. This file can then be uploaded to an Amazon S3 bucket. ```bash echo 'Hello, Amazon S3!' > example.txt ``` -------------------------------- ### Example Lambda Function for S3 Object Lambda (No Transformation) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/olap-using-cfn-template This is a basic AWS Lambda function that, when used with S3 Object Lambda, returns objects as-is from the underlying S3 data source. It serves as a starting point for custom transformations. The source code is available on GitHub. ```Python import boto3 import urllib.parse s3 = boto3.client('s3') def lambda_handler(event, context): print("Received event: " + str(event)) request_route = event['requestRoute'] get_object_context = event['getObjectContext'] if request_route == 'getObject': request_parameters = event['getObject']['requestParameters'] bucket_name = request_parameters['bucketName'] object_key = request_parameters['key'] # Retrieve the object from the original S3 bucket response = s3.get_object(Bucket=bucket_name, Key=object_key) object_data = response['Body'].read() content_type = response['ContentType'] # Return the object data to S3 Object Lambda s3.write_get_object_response( Bucket=get_object_context['outputBucket'], Key=get_object_context['outputOutput'] , Body=object_data, ContentType=content_type ) return { 'statusCode': 200, 'body': 'Object retrieved and returned successfully' } else: return { 'statusCode': 400, 'body': 'Unsupported request route' } ``` -------------------------------- ### Create Lambda Deployment Package (macOS) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-object-lambda-uppercase Packages the installed libraries from the virtual environment into a 'lambda.zip' file for deployment. This command navigates to the site-packages directory and zips its contents. ```bash cd venv/lib/python3.8/site-packages ``` ```bash zip -r ../../../../lambda.zip . ``` -------------------------------- ### S3 REST API: Retrieve Objects by Prefix Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/list-obj-version-enabled-bucket This example shows how to retrieve objects from an S3 bucket whose keys start with a specific prefix. It uses a GET request with the 'versions' and 'prefix' parameters. The Host header should be replaced with your bucket's domain. ```HTTP GET /?versions&prefix=myObject HTTP/1.1 Host: bucket.s3.amazonaws.com Date: Wed, 28 Oct 2009 22:32:00 GMT Authorization: AWS AKIAIOSFODNN7EXAMPLE:0RQf4/cRonhpaBX5sCYVf1bNRuU= ``` -------------------------------- ### Upload an Object to an S3 Bucket (AWS CLI) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GettingStartedS3CLI This command uploads a local file (`example.txt`) to a specified Amazon S3 bucket. The `aws s3 cp` command is used for copying files to and from S3. The output confirms the file transfer. ```bash aws s3 cp example.txt s3://amzn-s3-demo-bucket/ ``` -------------------------------- ### Verify Python Installation (Command Line) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-object-lambda-uppercase Commands to check if Python and pip are installed on your local machine. This ensures the correct version (3.8 or later) is available for the tutorial. ```bash python --version ``` ```bash python3 --version ``` ```bash pip --version ``` -------------------------------- ### AWS S3 Object GET StringToSign Example Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication This example demonstrates the construction of the StringToSign for an Object GET request to an AWS S3 bucket. It shows the mapping from the HTTP request headers and URI to the final StringToSign format, including positional elements like the Date header. ```http GET /photos/puppy.jpg HTTP/1.1 Host: awsexamplebucket1.us-west-1.s3.amazonaws.com Date: Tue, 27 Mar 2007 19:36:42 +0000 Authorization: AWS AKIAIOSFODNN7EXAMPLE: qgk2+6Sv9/oM7G3qLEjTH1a1l1g= ``` ```text GET Tue, 27 Mar 2007 19:36:42 +0000 /awsexamplebucket1/photos/puppy.jpg ``` -------------------------------- ### Create Lambda Deployment Package (Windows) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-object-lambda-uppercase Packages the installed libraries from the virtual environment into a 'lambda.zip' file for deployment using PowerShell. This command navigates to the site-packages directory and compresses its contents. ```powershell cd .\venv\Lib\site-packages\ ``` ```powershell Compress-Archive * ../../../lambda.zip ``` -------------------------------- ### Get GUID for Directory User/Group (AWS CLI) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-grant-create Commands to retrieve the GUID for directory users or groups from IAM Identity Center, which is required for creating access grants for them. ```APIDOC ## GET /list-users or /list-groups (AWS CLI) ### Description Retrieves the GUID (Identifier) for directory users or groups from an IAM Identity Center instance. This GUID is necessary when creating access grants for directory principals. ### Method AWS CLI Command ### Endpoint `aws identitystore list-users` or `aws identitystore list-groups` ### Parameters #### CLI Parameters - **--identity-store-id** (string) - Required - The ID of the IAM Identity Center instance. ### Request Example (List Users) ```bash aws identitystore list-users --identity-store-id d-1a2b3c4d1234 ``` ### Request Example (List Groups) ```bash aws identitystore list-groups --identity-store-id d-1a2b3c4d1234 ``` ### Response #### Success Response (200) Returns a list of users or groups with their details, including their unique identifier (GUID). #### Response Example (for list-users) ```json { "Users": [ { "UserName": "data-consumer-user", "UserId": "83d43802-00b1-7054-db02-f1d683aacba5" // ... other user details } ] // ... other response fields } ``` ``` -------------------------------- ### S3 GET Bucket (List Objects) Request Example Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/walkthrough1 An example HTTP request to list objects in an S3 bucket. It utilizes the `prefix` and `delimiter` parameters to organize and filter the results, typically used by the S3 console. ```http GET ?prefix=&delimiter=/ HTTP/1.1 Host: companybucket.s3.amazonaws.com Date: Wed, 01 Aug 2012 12:00:00 GMT Authorization: AWS AKIAIOSFODNN7EXAMPLE:xQE0diMbLRepdf3YB+FIEXAMPLE= ``` -------------------------------- ### Enable S3 Bucket Versioning (AWS CLI) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GettingStartedS3CLI Enables versioning for an S3 bucket. Versioning preserves and enables retrieval of multiple variants of an object, protecting against accidental deletions or overwrites. Requires `s3:PutBucketVersioning` permission. ```aws cli aws s3api put-bucket-versioning --bucket amzn-s3-demo-bucket --versioning-configuration Status=Enabled ``` -------------------------------- ### HTTP GET Request with Authorization Header Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting This example illustrates an HTTP GET request to the root of a custom domain aliased to an S3 bucket. It includes placeholder for the Date and Authorization headers, which are typically required for authenticated S3 requests. ```HTTP GET / HTTP/1.1 Host: www.example.com Date: date Authorization: signatureValue ``` -------------------------------- ### AWS CLI Configuration and Verification Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/policy-eval-walkthrough-download-awscli This snippet shows how to configure the AWS CLI with a default profile and verify the setup using basic commands. It includes examples for setting up credentials and listing S3 buckets. ```bash [default] aws_access_key_id = access key ID aws_secret_access_key = secret access key region = us-west-2 ``` ```bash aws help ``` ```bash aws s3 ls ``` ```bash [profile AccountAadmin] aws_access_key_id = User AccountAadmin access key ID aws_secret_access_key = User AccountAadmin secret access key region = us-west-2 [profile AccountBadmin] aws_access_key_id = Account B access key ID aws_secret_access_key = Account B secret access key region = us-east-1 ``` ```bash aws s3 ls s3://examplebucket --profile AccountBadmin ``` ```bash $ export AWS_DEFAULT_PROFILE=AccountAadmin ``` -------------------------------- ### Create S3 Buckets with GUIDs using AWS CLI Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects This code example shows how to create an Amazon S3 bucket with a globally unique identifier (GUID) using the AWS CLI, adhering to bucket naming conventions. ```aws cli aws s3 mb s3://your-bucket-name-$(openssl rand -hex 4) --region us-east-1 ``` -------------------------------- ### Verify Bucket Creation (AWS CLI) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GettingStartedS3CLI Lists all Amazon S3 buckets in the current account to verify successful bucket creation. This command does not require any parameters. ```bash aws s3 ls ``` -------------------------------- ### Download an Object from an S3 Bucket (AWS CLI) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/GettingStartedS3CLI This command downloads a specified object from an S3 bucket to the local file system. The `aws s3 cp` command handles the transfer. The output confirms the download, and the file can then be verified using `cat`. ```bash aws s3 cp s3://amzn-s3-demo-bucket/example.txt downloaded-example.txt ``` -------------------------------- ### Example Authenticated Amazon S3 REST Request Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication An example of an authenticated GET request to Amazon S3, demonstrating the structure of the HTTP request and the Authorization header. This format is crucial for verifying the request's integrity and the requester's identity. ```HTTP GET /photos/puppy.jpg HTTP/1.1 Host: awsexamplebucket1.us-west-1.s3.amazonaws.com Date: Tue, 27 Mar 2007 19:36:42 +0000 Authorization: AWS AKIAIOSFODNN7EXAMPLE: qgk2+6Sv9/oM7G3qLEjTH1a1l1g= ``` -------------------------------- ### S3 Lifecycle Rule Partial Prefix Example Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/troubleshoot-lifecycle This example demonstrates how to specify a partial prefix in an S3 Lifecycle rule to include multiple prefixes that start with the same characters. This is a workaround for the limitation that S3 Lifecycle does not support including multiple distinct prefixes directly. ```xml example-rule sales Enabled 30 STANDARD_IA ``` -------------------------------- ### Get Permissions for an Object Version (S3 GET) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/VersionedObjectPermissionsandACLs This example shows how to retrieve the Access Control List (ACL) for a specific version of an object in an Amazon S3 bucket. It requires specifying the object's key and version ID in the GET request. If the version ID is omitted, permissions for the current version are returned. ```http GET /my-image.jpg?acl&versionId=3HL4kqtJvjVBH40Nrjfkd HTTP/1.1 Host: bucket.s3.amazonaws.com Date: Wed, 28 Oct 2009 22:32:00 GMT Authorization: AWS AKIAIOSFODNN7EXAMPLE:0RQf4/cRonhpaBX5sCYVf1bNRuU= ``` -------------------------------- ### Install Lambda Function Dependencies Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-object-lambda-uppercase Installs the 'boto3' (AWS SDK for Python) and 'requests' libraries within the activated virtual environment. ```bash pip3 install boto3 ``` ```bash pip3 install requests ``` -------------------------------- ### S3 REST API: Retrieve Additional Objects (Truncated Response) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/list-obj-version-enabled-bucket This example illustrates how to retrieve subsequent objects when an initial GET request is truncated. It requires using 'key-marker' and 'version-id-marker' from the previous response to continue the listing. This is part of the 'GET Bucket versions' operation. ```HTTP GET /?versions&key-marker=myObject&version-id-marker=298459348571 HTTP/1.1 Host: bucket.s3.amazonaws.com Date: Wed, 28 Oct 2009 22:32:00 GMT Authorization: AWS AKIAIOSFODNN7EXAMPLE:0RQf4/cRonhpaBX5sCYVf1bNRuU= ``` -------------------------------- ### S3 Storage Lens Example Configuration in JSON Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/S3LensHelperFilesCLI This JSON file demonstrates a basic S3 Storage Lens Organizations-level configuration for advanced metrics and recommendations. Replace placeholder values with your specific information. ```json { "version": "2018-11-29", "statement": [ { "sid": "ExampleSSMPermissions", "effect": "Allow", "principal": { "Service": "storage-lens.amazonaws.com" }, "action": [ "s3:ListBucket", "s3:GetBucketLocation", "s3:ListBucketMultipartUploads" ], "resource": "arn:aws:s3:::example-bucket" } ] } ``` -------------------------------- ### Get Access Grant Java SDK Example Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants-grant-view Retrieves the details of a specific S3 Access Grant using the AWS SDK for Java. ```APIDOC ## Get Access Grant Details ### Description Retrieves the details of a specific S3 Access Grant, identified by its Account ID and Access Grant ID. ### Method `s3Control.getAccessGrant()` (AWS SDK for Java) ### Endpoint N/A (AWS SDK method) ### Parameters #### Request Body (Implicit via SDK object) - **accountId** (string) - Required - The AWS account ID associated with the S3 Access Grants instance. - **accessGrantId** (string) - Required - The unique identifier for the access grant to retrieve. ### Request Example (Java SDK) ```java public void getAccessGrant() { GetAccessGrantRequest getRequest = GetAccessGrantRequest.builder() .accountId("111122223333") .accessGrantId("a1b2c3d4-5678-90ab-cdef-EXAMPLE22222") .build(); GetAccessGrantResponse getResponse = s3Control.getAccessGrant(getRequest); LOGGER.info("GetAccessGrantResponse: " + getResponse); } ``` ### Response #### Success Response (200) - **AccessGrantId** (string) - The unique identifier for the access grant. - **AccessGrantArn** (string) - The Amazon Resource Name (ARN) of the access grant. - **Grantee** (object) - Details about the grantee. - **GranteeType** (string) - The type of grantee (e.g., `IAM`). - **GranteeIdentifier** (string) - The identifier for the grantee. - **Permission** (string) - The permission level granted (e.g., `READ`). - **GrantScope** (string) - The scope of the grant, typically an S3 path. #### Response Example (Java SDK Output) ``` GetAccessGrantResponse( CreatedAt=2023-06-07T05:20:26.330Z, AccessGrantId=a1b2c3d4-5678-90ab-cdef-EXAMPLE22222, AccessGrantArn=arn:aws:s3:us-east-2:111122223333:access-grants/default/grant-fd3a5086-42f7-4b34-9fad-472e2942c70e, Grantee=Grantee( GranteeType=IAM, GranteeIdentifier=arn:aws:iam::111122223333:user/data-consumer-3 ), Permission=READ, AccessGrantsLocationId=12a6710f-5af8-41f5-b035-0bc795bf1a2b, AccessGrantsLocationConfiguration=AccessGrantsLocationConfiguration( S3SubPrefix=prefixB* ), GrantScope=s3://amzn-s3-demo-bucket/ ) ``` ``` -------------------------------- ### Manage S3 CORS Configuration (AWS SDK for .NET) Source: https://docs.aws.amazon.com/AmazonS3/latest/userguide/enabling-cors-examples This .NET example demonstrates the complete lifecycle of managing S3 CORS configurations. It shows how to initialize the S3 client, put a new CORS configuration with multiple rules, retrieve the configuration, add another rule, verify the count, and finally delete the configuration. Error handling for S3 and general exceptions is included. ```csharp using Amazon; using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class CORSTest { private const string bucketName = "*** bucket name ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; public static void Main() { s3Client = new AmazonS3Client(bucketRegion); CORSConfigTestAsync().Wait(); } private static async Task CORSConfigTestAsync() { try { // Create a new configuration request and add two rules CORSConfiguration configuration = new CORSConfiguration { Rules = new System.Collections.Generic.List { new CORSRule { Id = "CORSRule1", AllowedMethods = new List {"PUT", "POST", "DELETE"}, AllowedOrigins = new List {"http://*.example.com"} }, new CORSRule { Id = "CORSRule2", AllowedMethods = new List {"GET"}, AllowedOrigins = new List {"*"}, MaxAgeSeconds = 3000, ExposeHeaders = new List {"x-amz-server-side-encryption"} } } }; // Add the configuration to the bucket. await PutCORSConfigurationAsync(configuration); // Retrieve an existing configuration. configuration = await RetrieveCORSConfigurationAsync(); // Add a new rule. configuration.Rules.Add(new CORSRule { Id = "CORSRule3", AllowedMethods = new List { "HEAD" }, AllowedOrigins = new List { "http://www.example.com" } }); // Add the configuration to the bucket. await PutCORSConfigurationAsync(configuration); // Verify that there are now three rules. configuration = await RetrieveCORSConfigurationAsync(); Console.WriteLine(); Console.WriteLine("Expected # of rulest=3; found:{0}", configuration.Rules.Count); Console.WriteLine(); Console.WriteLine("Pause before configuration delete. To continue, click Enter..."); Console.ReadKey(); // Delete the configuration. await DeleteCORSConfigurationAsync(); // Retrieve a nonexistent configuration. configuration = await RetrieveCORSConfigurationAsync(); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } static async Task PutCORSConfigurationAsync(CORSConfiguration configuration) { PutCORSConfigurationRequest request = new PutCORSConfigurationRequest { BucketName = bucketName, Configuration = configuration }; var response = await s3Client.PutCORSConfigurationAsync(request); } static async Task RetrieveCORSConfigurationAsync() { GetCORSConfigurationRequest request = new GetCORSConfigurationRequest { BucketName = bucketName }; var response = await s3Client.GetCORSConfigurationAsync(request); return response.Configuration; } static async Task DeleteCORSConfigurationAsync() { DeleteCORSConfigurationRequest request = new DeleteCORSConfigurationRequest { BucketName = bucketName }; await s3Client.DeleteCORSConfigurationAsync(request); } } } ```