Use the AWS SDK for Ruby with Backblaze B2
    • Dark
      Light

    Use the AWS SDK for Ruby with Backblaze B2

    • Dark
      Light

    Article Summary

    You can take advantage of Backblaze B2 Cloud Storage using the AWS SDK for Ruby alongside the Backblaze S3-Compatible API.

    You need to set the client endpoint and region to the S3 endpoint and region of the Backblaze B2 account to which you are connecting, along with supplying an Application Key and Key ID for the account.

    To use the AWS SDK for Ruby with Backblaze B2, you must:

    • Use version 1.131.0 or later of the aws-sdk-s3 gem. Older versions do not support easy configuration of a custom endpoint using the service-specific endpoint feature. 
    • Configure the AWS_REGION environment variable or the region setting in the shared AWS config file with the region segment of your bucket’s S3 endpoint. The region segment is located between the s3. and .backblazeb2.com in the S3 endpoint URL. For example, if your endpoint is s3.us-west-004.backblazeb2.com, the region is us-west-004.
    • Configure the AWS_ENDPOINT_URL environment variable or the endpoint_url setting in the shared AWS config file with your bucket’s S3 endpoint. Note: you must include the https:// prefix in the URL, for example https://s3.us-west-004.backblazeb2.com.
    • Configure your application key and application key ID via one of the methods supported by the AWS SDK for Ruby. Note: the Backblaze application key ID is equivalent to the AWS access key id and the Backblaze application key itself is equivalent to the AWS secret access key.

    The following example demonstrates how to create a bucket and upload data after having configured the AWS_REGION, AWS_ENDPOINT_URL, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY using either environment variables or the shared AWS config file:

    require 'aws-sdk-s3'
    
    # Use aws-sdk-s3 gem version 1.131.0 or later to configure endpoint via
    # AWS_ENDPOINT_URL environment variable
    s3 = Aws::S3::Client.new()
    
    s3.create_bucket(bucket: '<bucket-name>')
    
    s3.put_object(
      bucket: '<bucket-name>', 
      key: 'hello.txt', 
      body: 'Hello, World!'
    )
    


    The S3-Compatible API allows 1000’s of integrations to natively work with Backblaze B2. If you are new to the S3-Compatible API, click here. If you have issues using this SDK with Backblaze B2, let us know by emailing us at [email protected].



    Was this article helpful?