### Create Presigned S3 URL Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Example of creating a presigned URL for S3 operations (e.g., PUT, GET). It involves defining options like keys, region, bucket, object, and additional headers. Support for v2 signing is also included. ```crystal options = Awscr::S3::Presigned::Url::Options.new( aws_access_key: "key", aws_secret_key: "secret", region: "us-east-1", object: "test.txt", bucket: "mybucket", additional_options: { "Content-Type" => "image/png" }) url = Awscr::S3::Presigned::Url.new(options) puts url.for(:put) ``` ```crystal options = Awscr::S3::Presigned::Url::Options.new( aws_access_key: "key", aws_secret_key: "secret", region: "us-east-1", object: "test.txt", bucket: "mybucket", signer: :v2 ) ``` -------------------------------- ### Create S3 Bucket Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Example of creating a new S3 bucket. The function takes the desired bucket name and returns true if the creation is successful. ```crystal client = Client.new("region", "key", "secret") resp = client.put_bucket("test") resp # => true ``` -------------------------------- ### Install awscr-s3 Shard Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Instructions for adding the awscr-s3 shard to your Crystal project's shard.yml file. This is the primary step for including the library in your application. ```yaml dependencies: awscr-s3: github: taylorfinnell/awscr-s3 ``` -------------------------------- ### List Objects in S3 Bucket Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Example of listing objects within an S3 bucket. The code iterates through the response to print the keys of the contents. ```crystal client.list_objects("bucket_name").each do |resp| p resp.contents.map(&.key) end ``` -------------------------------- ### Generate Presigned URLs for S3 Operations Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Shows how to generate time-limited URLs for GET and PUT operations, enabling direct client-side interaction with S3, Minio, or DigitalOcean Spaces without exposing credentials. ```crystal require "awscr-s3" options = Awscr::S3::Presigned::Url::Options.new( aws_access_key: "AKIAIOSFODNN7EXAMPLE", aws_secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", region: "us-east-1", bucket: "my-bucket", object: "documents/report.pdf", expires: 3600 ) url = Awscr::S3::Presigned::Url.new(options) get_url = url.for(:get) put_url = url.for(:put) ``` -------------------------------- ### Get Object from S3 Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Illustrates retrieving an object from an S3 bucket. It shows how to get the object body directly or stream it for large objects using a block. ```crystal resp = client.get_object("bucket_name", "object_key") resp.body # => myobjectbody ``` ```crystal client.get_object("bucket_name", "object_key") do |obj| IO.copy(obj.body_io, STDOUT) # => myobjectbody end ``` -------------------------------- ### Check S3 Bucket Existence Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md This example shows how to check if an S3 bucket exists. It returns true if the bucket is found, otherwise it raises an exception. ```crystal resp = client.head_bucket("bucket_name") resp # => true ``` -------------------------------- ### GET /get_object Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Retrieves an object's content from an S3 bucket. Supports direct retrieval and streaming for large files. ```APIDOC ## GET /get_object ### Description Retrieves an object's content from an S3 bucket. For large files, use the block form to stream the content without loading it entirely into memory. ### Method GET ### Endpoint /get_object ### Parameters #### Path Parameters - **bucket** (string) - Required - The name of the S3 bucket. - **object** (string) - Required - The key of the object to retrieve. ### Request Example client.get_object("my-bucket", "hello.txt") ### Response #### Success Response (200) - **body** (string/IO) - The content of the S3 object. #### Response Example { "body": "Hello, World!" } ``` -------------------------------- ### Delete S3 Bucket Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Code example for deleting an S3 bucket. It requires the bucket name and returns true upon successful deletion. ```crystal client = Client.new("region", "key", "secret") resp = client.delete_bucket("test") resp # => true ``` -------------------------------- ### Get Object from S3 Bucket Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Retrieves an object's content from an S3 bucket. Supports streaming for large files and custom headers like range requests. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") # Get object content directly resp = client.get_object("my-bucket", "hello.txt") puts resp.body # => "Hello, World!" # Stream large objects (recommended for large files) client.get_object("my-bucket", "large-file.zip") do |resp| File.open("downloaded.zip", "w") do |file| IO.copy(resp.body_io, file) end end # Get with custom headers (e.g., range requests) resp = client.get_object( bucket: "my-bucket", object: "video.mp4", headers: {"Range" => "bytes=0-1023"} ) ``` -------------------------------- ### Get Object Metadata (Head Object) from S3 Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Retrieves metadata for an S3 object without downloading its content. Returns size, ETag, last modified date, and custom metadata. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") resp = client.head_object("my-bucket", "document.txt") puts resp.size # => 1234 puts resp.status # => HTTP::Status::OK puts resp.last_modified # => "Wed, 19 Jun 2019 11:55:33 GMT" puts resp.etag # => "\"5eb63bbbe01eeed093cb22bb8f5acdc3\"" puts resp.meta # => {"author" => "John Doe", "version" => "1.0"} ``` -------------------------------- ### Configure S3-compatible services in Crystal Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Demonstrates how to initialize presigned URL options for S3-compatible services by providing custom endpoints. This approach supports local Minio instances and cloud providers like DigitalOcean Spaces. ```crystal options = Awscr::S3::Presigned::Url::Options.new( endpoint: "http://127.0.0.1:9000", region: "unused", aws_access_key: "admin", aws_secret_key: "password", bucket: "foo", force_path_style: true, object: "/test.txt" ) url = Awscr::S3::Presigned::Url.new(options) puts url.for(:get) ``` ```crystal options = Awscr::S3::Presigned::Url::Options.new( endpoint: "https://ams3.digitaloceanspaces.com", region: "unused", aws_access_key: "ACCESSKEYEXAMPLE", aws_secret_key: "SECRETKEYEXAMPLE", bucket: "foo", object: "/test.txt" ) url = Awscr::S3::Presigned::Url.new(options) puts url.for(:get) ``` -------------------------------- ### Run project test suites Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Provides CLI commands for executing unit and integration tests. Integration tests require external dependencies managed via Docker Compose. ```shell # Run all tests crystal spec # Run only unit tests crystal spec --tag '~integration' # Start integration dependencies docker compose -f spec/integration/compose.yml up -d # Run integration tests crystal spec --tag 'integration' ``` -------------------------------- ### Create S3 Client Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Demonstrates how to instantiate an Awscr::S3::Client. It covers standard client creation with region, key, and secret, as well as configuration for S3-compatible services with a custom endpoint and support for v2 request signing. ```crystal client = Awscr::S3::Client.new("us-east-1", "key", "secret") ``` ```crystal client = Awscr::S3::Client.new("nyc3", "key", "secret", endpoint: "https://nyc3.digitaloceanspaces.com") ``` ```crystal client = Awscr::S3::Client.new("us-east-1", "key", "secret", signer: :v2) ``` -------------------------------- ### Create awscr-s3 Client Instances Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Instantiate the `Awscr::S3::Client` with various configurations, including standard AWS S3, temporary credentials, S3-compatible services like DigitalOcean Spaces and Minio, and different signer versions. ```crystal require "awscr-s3" # Standard AWS S3 client client = Awscr::S3::Client.new( region: "us-east-1", aws_access_key: "AKIAIOSFODNN7EXAMPLE", aws_secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" ) # Client with temporary credentials (session token) client = Awscr::S3::Client.new( region: "us-east-1", aws_access_key: "key", aws_secret_key: "secret", aws_session_key: "session_token" ) # Client for DigitalOcean Spaces client = Awscr::S3::Client.new( region: "nyc3", aws_access_key: "key", aws_secret_key: "secret", endpoint: "https://nyc3.digitaloceanspaces.com" ) # Client for Minio (local S3-compatible storage) client = Awscr::S3::Client.new( region: "unused", aws_access_key: "admin", aws_secret_key: "password", endpoint: "http://127.0.0.1:9000" ) # Client with V2 signer (for legacy compatibility) client = Awscr::S3::Client.new( region: "us-east-1", aws_access_key: "key", aws_secret_key: "secret", signer: :v2 ) ``` -------------------------------- ### Initialize S3 Client with Custom Connection Pool Factory Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Demonstrates how to create an Awscr::S3::Client instance using a custom PoolingHttpClientFactory for managing HTTP connections. This allows for connection reuse, improving performance for multiple S3 operations. ```crystal factory = PoolingHttpClientFactory.new(pool_size: 10) client = Awscr::S3::Client.new( region: "us-east-1", aws_access_key: "key", aws_secret_key: "secret", client_factory: factory ) # Multiple operations reuse pooled connections client.list_buckets client.put_object("bucket", "key", "data") client.get_object("bucket", "key") ``` -------------------------------- ### List S3 Buckets Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Shows how to list all available S3 buckets using the Awscr::S3::Client. The response contains a list of bucket names. ```crystal resp = client.list_buckets resp.buckets # => ["bucket1", "bucket2"] ``` -------------------------------- ### Create Presigned HTML Upload Forms Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Explains how to build secure, policy-validated HTML forms for direct browser-to-S3 uploads. Includes support for V2 signing and custom conditions. ```crystal require "awscr-s3" require "uuid" form = Awscr::S3::Presigned::Form.build( region: "us-east-1", aws_access_key: "AKIAIOSFODNN7EXAMPLE", aws_secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" ) do |f| f.expiration(Time.utc + 1.hour) f.condition("bucket", "my-upload-bucket") f.condition("key", "uploads/#{UUID.random}.png") end puts form.to_html ``` -------------------------------- ### Create an S3 Bucket Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt The `put_bucket` method creates a new S3 bucket. Optionally specify a region for bucket creation and pass custom headers. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") # Create bucket in default region result = client.put_bucket("my-new-bucket") puts result # => true # Create bucket in a specific region result = client.put_bucket("my-eu-bucket", region: "eu-west-1") puts result # => true ``` -------------------------------- ### Implement custom HTTP client factory Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Shows how to extend Awscr::S3::HttpClientFactory to manage custom connection pooling. This allows developers to reuse HTTP clients instead of creating new ones for every request. ```crystal require "awscr-s3" require "http" class PoolingHttpClientFactory < Awscr::S3::HttpClientFactory getter pool : Array(HTTP::Client) @created_count : Int32 = 0 def initialize(@pool_size : Int32 = 3) @pool = [] of HTTP::Client end def acquire_raw_client(endpoint : URI) : HTTP::Client if @pool.size > 0 @pool.pop elsif @created_count < @pool_size @created_count += 1 HTTP::Client.new(endpoint) else raise "No available clients in pool (limit of #{@pool_size} reached)" end end def release(client : HTTP::Client?) return unless client @pool << client end end ``` -------------------------------- ### Manual Multipart Upload to S3 Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Allows for fine-grained control over multipart uploads using the client's direct methods. Useful for resumable uploads or custom part management. ```crystal require "awscr-s3" require "uuid" client = Awscr::S3::Client.new("us-east-1", "key", "secret") object_key = UUID.random.to_s ``` -------------------------------- ### Create Presigned S3 Form Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Demonstrates building a presigned form for S3 uploads. It allows setting expiration, conditions, and can be configured for v2 signing. The form can then be converted to HTML or submitted. ```crystal form = Awscr::S3::Presigned::Form.build("us-east-1", "access key", "secret key") do |form| form.expiration(Time.unix(Time.now.to_unix + 1000)) form.condition("bucket", "mybucket") form.condition("acl", "public-read") form.condition("key", SecureRandom.uuid) form.condition("Content-Type", "text/plain") form.condition("success_action_status", "201") end ``` ```crystal form = Awscr::S3::Presigned::Form.build("us-east-1", "access key", "secret key", signer: :v2) do |form| ... end ``` ```crystal puts form.to_html ``` ```crystal data = IO::Memory.new("Hello, S3!") form.submit(data) ``` -------------------------------- ### Perform Multipart Uploads with awscr-s3 Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Demonstrates the multipart upload workflow, including initiating the upload, uploading individual parts, and finalizing the process. This is essential for handling large files efficiently. ```crystal # Start multipart upload upload = client.start_multipart_upload( bucket: "my-bucket", object: object_key ) puts "Upload ID: #{upload.upload_id}" # Upload parts (minimum 5MB per part except last) part1 = client.upload_part( bucket: "my-bucket", object: object_key, upload_id: upload.upload_id, part_number: 1, part: IO::Memory.new("A" * 5_000_001) ) part2 = client.upload_part( bucket: "my-bucket", object: object_key, upload_id: upload.upload_id, part_number: 2, part: IO::Memory.new("B" * 1000) ) # Complete the multipart upload result = client.complete_multipart_upload( bucket: "my-bucket", object: object_key, upload_id: upload.upload_id, parts: [part1, part2] ) puts "Completed: #{result.key}, ETag: #{result.etag}" ``` -------------------------------- ### POST /copy_object Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Copies an object from one location to another within the same bucket. ```APIDOC ## POST /copy_object ### Description Copies an object from one location to another within the same bucket. You can pass additional headers for metadata modification. ### Method POST ### Endpoint /copy_object ### Parameters #### Request Body - **bucket** (string) - Required - The bucket name. - **source** (string) - Required - The source object key. - **destination** (string) - Required - The destination object key. ``` -------------------------------- ### List Objects in S3 Bucket with Pagination and Filtering Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Returns an iterator for paginating through objects in an S3 bucket. Supports filtering by prefix, limiting results per page, and sorting by last modified date. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") # List all objects in bucket client.list_objects("my-bucket").each do |response| response.contents.each do |object| puts "#{object.key} - #{object.size} bytes - #{object.last_modified}" end end # List with prefix filter and max keys client.list_objects(bucket: "my-bucket", prefix: "images/", max_keys: 100).each do |response| keys = response.contents.map(&.key) puts keys # => ["images/photo1.jpg", "images/photo2.png"] end # Sort by last modified client.list_objects(bucket: "my-bucket", max_keys: 10).each do |response| sorted_keys = response.contents.sort_by(&.last_modified).map(&.key) puts sorted_keys end ``` -------------------------------- ### Put Object to S3 Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Demonstrates uploading an object to an S3 bucket. It includes uploading the object body and optionally adding custom headers like metadata. ```crystal resp = client.put_object("bucket_name", "object_key", "myobjectbody") resp.etag # => ... ``` ```crystal client.put_object("bucket_name", "object_key", "myobjectbody", {"x-amz-meta-name" => "myobject"}) ``` -------------------------------- ### POST /batch_delete Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Removes multiple objects in a single request. ```APIDOC ## POST /batch_delete ### Description Removes multiple objects in a single request (up to 1000 keys). This is more efficient than deleting objects individually. ### Method POST ### Endpoint /batch_delete ### Parameters #### Request Body - **bucket** (string) - Required - The name of the S3 bucket. - **keys** (array) - Required - List of object keys to delete. ### Response #### Success Response (200) - **success** (boolean) - Indicates if the batch request was processed. - **deleted** (array) - List of objects successfully deleted. ``` -------------------------------- ### Upload File to S3 Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Shows how to upload a local file to an S3 bucket using Awscr::S3::FileUploader. It supports specifying additional headers like metadata. ```crystal uploader = Awscr::S3::FileUploader.new(client) File.open(File.expand_path("myfile"), "r") do |file| puts uploader.upload("bucket_name", "someobjectkey", file) end ``` ```crystal uploader = Awscr::S3::FileUploader.new(client) File.open(File.expand_path("myfile"), "r") do |file| puts uploader.upload("bucket_name", "someobjectkey", file, {"x-amz-meta-name" => "myobject"}) end ``` -------------------------------- ### HEAD /head_object Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Retrieves metadata about an object without downloading its content. ```APIDOC ## HEAD /head_object ### Description Retrieves metadata about an object without downloading its content. It returns size, ETag, last modified date, and custom metadata. ### Method HEAD ### Endpoint /head_object ### Parameters #### Path Parameters - **bucket** (string) - Required - The name of the S3 bucket. - **object** (string) - Required - The key of the object. ### Response #### Success Response (200) - **size** (integer) - Size of the object in bytes. - **etag** (string) - The ETag of the object. - **last_modified** (string) - Last modified timestamp. - **meta** (hash) - Custom metadata associated with the object. ``` -------------------------------- ### Upload Files to S3 with FileUploader Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Provides a high-level interface for uploading files to S3. Automatically uses multipart uploads for large files and handles content-type detection. Supports custom metadata and upload options. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") uploader = Awscr::S3::FileUploader.new(client) # Upload a local file File.open("./my-image.png", "r") do |file| result = uploader.upload("my-bucket", "uploads/my-image.png", file) puts result # => true end # Upload with custom metadata File.open("./document.pdf", "r") do |file| uploader.upload( bucket: "my-bucket", object: "documents/report.pdf", io: file, headers: { "x-amz-meta-title" => "Annual Report", "x-amz-meta-year" => "2024" } ) end # Custom uploader options (disable auto content-type) uploader = Awscr::S3::FileUploader.new( client, Awscr::S3::FileUploader::Options.new( with_content_types: false, simultaneous_parts: 10, # Concurrent upload parts multipart_threshold: 10_000_000 # 10MB threshold ) ) ``` -------------------------------- ### Check S3 Bucket Existence Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt The `head_bucket` method verifies if an S3 bucket exists and is accessible. It raises an `Http::ServerError` if the bucket is not found or access is denied. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") begin result = client.head_bucket("my-bucket") puts "Bucket exists: #{result}" # => true rescue ex : Awscr::S3::Http::ServerError puts "Bucket does not exist or access denied" end ``` -------------------------------- ### Upload Object to S3 Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt The `put_object` method uploads data to an S3 bucket. It supports string, IO, or byte array bodies and allows setting custom metadata and content type headers. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") # Upload a string resp = client.put_object("my-bucket", "hello.txt", "Hello, World!") puts resp.etag # => "\"5eb63bbbe01eeed093cb22bb8f5acdc3\"" # Upload with metadata headers resp = client.put_object( bucket: "my-bucket", object: "document.txt", body: "Document content", headers: { "x-amz-meta-author" => "John Doe", "x-amz-meta-version" => "1.0", "Content-Type" => "text/plain" } ) # Upload from IO io = IO::Memory.new("Binary data here") resp = client.put_object("my-bucket", "data.bin", io) ``` -------------------------------- ### Batch Delete S3 Objects Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Demonstrates deleting multiple objects from an S3 bucket in a single operation. It takes the bucket name and an array of object keys, returning a success status. ```crystal resp = client.batch_delete("bucket_name", ["key1", "key2"]) resp.success? # => true ``` -------------------------------- ### DELETE /delete_object Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Removes a single object from an S3 bucket. ```APIDOC ## DELETE /delete_object ### Description Removes a single object from an S3 bucket. Returns true if successful. ### Method DELETE ### Endpoint /delete_object ### Parameters #### Path Parameters - **bucket** (string) - Required - The name of the S3 bucket. - **object** (string) - Required - The key of the object to delete. ### Response #### Success Response (200) - **result** (boolean) - Returns true if the deletion was successful. ``` -------------------------------- ### Batch Delete Multiple Objects from S3 Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Removes multiple objects from an S3 bucket in a single request, supporting up to 1000 keys for efficiency. Provides details on deleted objects and any errors. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") keys_to_delete = ["file1.txt", "file2.txt", "folder/file3.txt"] resp = client.batch_delete("my-bucket", keys_to_delete) puts resp.success? # => true # Check for any errors resp.deleted.each do |deleted| puts "Deleted: #{deleted.key}" end ``` -------------------------------- ### Copy Object within S3 Bucket Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Copies an object to a new location within the same S3 bucket. Allows for modification of metadata during the copy process. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") resp = client.copy_object( bucket: "my-bucket", source: "original/document.txt", destination: "backup/document.txt" ) puts resp.etag # => ETag of the copied object ``` -------------------------------- ### Require awscr-s3 Library Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md This code snippet shows how to require the awscr-s3 library in your Crystal project. This is necessary before using any of its functionalities. ```crystal require "awscr-s3" ``` -------------------------------- ### Delete an S3 Bucket Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Use `delete_bucket` to remove an empty S3 bucket. This operation will fail if the bucket contains any objects. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") result = client.delete_bucket("my-bucket-to-delete") puts result # => true ``` -------------------------------- ### Delete Object from S3 Source: https://github.com/taylorfinnell/awscr-s3/blob/master/README.md Code snippet for deleting an object from an S3 bucket. It requires the bucket name and object key, returning true on success. ```crystal resp = client.delete_object("bucket_name", "object_key") resp # => true ``` -------------------------------- ### Delete Single Object from S3 Bucket Source: https://context7.com/taylorfinnell/awscr-s3/llms.txt Removes a single object from an S3 bucket. Returns true if the deletion was successful. ```crystal require "awscr-s3" client = Awscr::S3::Client.new("us-east-1", "key", "secret") result = client.delete_object("my-bucket", "old-file.txt") puts result # => true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.