AWS SDK

Python

Boto

boto3 is a software development kit (SDK) for the Python 3.x programming languages. SDK are designed for working with the AWS services.

To install boto, follow the instructions in the corresponding boto3 repositories.

To configure it, create configuration files in your home directory and set the following parameters:

  • static key in the ~/.aws/credentials:

    [default]
    aws_access_key_id = AccountNumber_UserName
    aws_secret_access_key = Password
  • default region in ~/.aws/config:

    [default]
    region=ru-1a

Examples

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import boto3

# Authorization
s3 = boto3.client(
   service_name='s3',
   endpoint_url='https://s3.selcdn.ru'
)

# Uploading an object from string
s3.put_object(Bucket='BucketName', Key='ObjectName1', Body='Test')

# Uploading an object from file
s3.upload_file('data.docx', 'BucketName', 'ObjectName2')

# Getting a list of objects in the bucket
for key in s3.list_objects(Bucket='BucketName')['Contents']:
   print(key['Key'])

# Downloading an object
get_object_response = s3.get_object(Bucket='BucketName', Key='ObjectName2')
print(get_object_response['Body'].read())

# Deleting multiple objects
objects_to_delete = [{'Key': 'ObjectName1'}, {'Key': 'ObjectName2'}]
s3.delete_objects(Bucket='BucketName', Delete={'Objects': objects_to_delete})

Example for boto3

Create an s3 client by providing endpoint_url, region, and user data on whose behalf the objects will be uploaded:

s3 = boto3.client('s3', endpoint_url='https://s3.selcdn.ru', region_name='ru-1a', aws_access_key_id='user', aws_secret_access_key='user_pass')

Call the generate_presigned_post() method by passing the name of the bucket to upload to and the key name, which may contain the ${filename} template for the filename provided by the user when uploading. This call will return a dictionary with the url to which the form should be sent, and with the fields dictionary with all the required fields filled in for this form (X-Amz-Algorithm, X-Amz-Credential, X-Amz-Date, X-Amz-Signature, Policy).

post = s3.generate_presigned_post(bucket, key)

If you need to add additional fields to the object and form (for example, the Content-Type header), then pass a dictionary with these fields as the Fields argument. Also, according to the Policy specification, you need to describe each additional field in the conditions array (the Conditions argument in boto).

post = s3.generate_presigned_post(bucket, key,
                                  Fields={"Content-Type": "image/webp"},
                                  Conditions=[["eq", "$content-type", "image/webp"]])

You can compose an HTML form from the received data or reproduce the request via requests:

requests.post(post['url'], data=post['fields'], files=[("file", ('filename', b'body_data'))])

When generating an HTML form, you need to keep in mind that the file field with the data of the uploaded file must be at the end of the form.

PHP

AWS SDK for PHP is a software development kit for working with the AWS services.

The SDK is a modern, open source PHP library that makes it easy to integrate your PHP application with the S3 object storage.

Follow these steps to connect the library using the Composer dependency manager:

  1. Install Composer:

    curl -sS https://getcomposer.org/installer | php
  2. Run Composer to install the latest stable SDK version:

    php composer.phar require aws/aws-sdk-php
  3. Include aws-sdk-php in your script.

Access key ID (AccountNumber_UserName) and Secret access key (Password) are required for authorization.

Examples

<?php
require 'vendor/autoload.php';
 
use Aws\S3\S3Client;
// Creating a client
$s3Client = new S3Client([
   'version' 	=> 'latest',
   'region'  	=> 'ru-1a',
   'use_path_style_endpoint' => true,
   'credentials' => [
   	'key'	=> 'AccountNumber_UserName',
   	'secret' => 'Password',
   ],
   'endpoint' => 'https://s3.selcdn.ru'
]);
 
// Uploading an object from string
$s3Client->putObject([
   'Bucket' => 'BucketName',
   'Key'	=> 'ObjectName',
   'Body'   => 'Test'
]);
 
// Uploading an object
$result = $s3Client->getObject([
   'Bucket' => 'BucketName',
   'Key'	=> 'ObjectName'
]);
 
echo $result['Body'];

JavaScript

AWS SDK for Node JS is a set of development tools for running JavaScript with the AWS services in Node.js environment.

Example for Node JS

var S3 = require('aws-sdk/clients/s3');

var s3 = new S3({
    accessKeyId: 'AccountNumber_UserName',
    secretAccessKey: 'Password',
    endpoint: 'https://s3.selcdn.ru',
    s3ForcePathStyle: true,
    region: 'ru-1a',
    apiVersion: 'latest'
});

// Uploading an object

var params = {
    Bucket: 'BucketName',
    Key: 'ObjectName',
    Body: 'Test'
};

s3.upload(params, (err, data) => {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data); 
    }
    /*
    data = {
        ETag: '0cbc6611f5540bd0809a388dc95a615b',
        Location: 'https://s3.selcdn.ru/BucketName/ObjectName',
        key: 'ObjectName',
        Key: 'ObjectName',
        Bucket: 'BucketName'
    }
    */

});

// Retrieving object metadata

var params = {
    Bucket: 'BucketName',
    Key: 'ObjectName'
};

s3.headObject(params, (err, data) => {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data); 
    }
    /*
    data = {
        AcceptRanges: 'bytes',
        LastModified: 2019-12-03T17:29:15.000Z,
        ContentLength: 4,
        ETag: '0cbc6611f5540bd0809a388dc95a615b',
        ContentType: 'application/octet-stream',
        Metadata: {}
    }
    */
});

// Retrieving an object

var params = {
    Bucket: 'BucketName',
    Key: 'ObjectName'
};

s3.getObject(params, (err, data) => {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data); 
    }
    /*
    data = {
        AcceptRanges: 'bytes',
        LastModified: 2019-12-03T17:29:15.000Z,
        ContentLength: 4,
        ETag: '0cbc6611f5540bd0809a388dc95a615b',
        ContentType: 'application/octet-stream',
        Metadata: {},
        Body: <Buffer 54 65 73 74>
    }
    */
});


// Deleting an object

var params = {
    Bucket: 'BucketName',
    Key: 'ObjectName'
};

s3.deleteObject(params, (err, data) => {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data); 
    }
    /*
    data = {
    }
    */
});

Java

// Configure S3 client connection
AWSCredentials credentials = new BasicAWSCredentials(
        "<access_key>",
        "<secret_key>"
);

EndpointConfiguration endpoint =
        new EndpointConfiguration("https://s3.selcdn.ru", "ru-1a");

AmazonS3 s3client = AmazonS3ClientBuilder
        .standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .withPathStyleAccessEnabled(true)
        .withEndpointConfiguration(endpoint)
        .build();

// Create bucket
String bucketName = "s3bucket";
String objectName = "s3object";

if(!s3client.doesBucketExistV2(bucketName)) {
        s3client.createBucket(bucketName);
}

// Upload object
s3client.putObject(
        bucketName,
        objectName,
        "sample-data"
);

// Download object
S3Object s3object = s3client.getObject(bucketName, objectName);
S3ObjectInputStream inputStream = s3object.getObjectContent();

inputStream.transferTo(new FileOutputStream("downloaded-object"));

// Delete object
s3client.deleteObject(bucketName, objectName);

// Delete bucket
s3client.deleteBucket(bucketName);