Getting started with Pilvio S3 for developers (AWS-SDK)

If you want application you develop to work with files on S3 – AWS-SDK is the best way to go.
On the internet, there are a lot of various examples and code snippets of using aws-sdk.
This SDK has good documentation, big community and huge support. These factors guarantee that newcomers can have a quick start on learning and using this technology.
Our goal was to make your start with Pilvio S3 and AWS-SDK even smoother and faster.

We prepared minimalistic “starter” examples for the following languages:
1) JavaScript
2) Golang
3) PHP
4) Python
5) Ruby
6) Perl
(we will add examples for more languages soon)

Do not forget to set “BUCKET_NAME”, “ACCESS_KEY_ID”, “SECRET_ACCESS_KEY” constants with your values.

Official documentation: https://docs.aws.amazon.com/sdk-for-javascript/index.html
Useful materials also can be found here: https://github.com/awsdocs/aws-doc-sdk-examples

 


JavaScript

Setup:

npm install aws-sdk

 

'use strict'

const AWS = require('aws-sdk')

const ACCESS_KEY_ID = "NqTqcpjnNpCTW3bpMrcdN"
const SECRET_ACCESS_KEY = "XJx4sxLM9v3TsmKVdTwMJLhPFFxtrMhpJRNRqsgV"
const BUCKET_NAME = "js-example"
const ENDPOINT = "https://s3.pilw.io"

const s3 = new AWS.S3({
       accessKeyId: ACCESS_KEY_ID,
       secretAccessKey: SECRET_ACCESS_KEY,
       endpoint: new AWS.Endpoint(ENDPOINT)
})

let params = {
       Key: 'hello.txt',
       Body: 'Hello world',
       Bucket: BUCKET_NAME
}

s3.upload(params).promise().then((result) => {
     console.log(result)
}).catch((err) => {
     console.log(err)
})

 


Golang

Setup:

go get -u github.com/aws/aws-sdk-go

 

package main

import (
      "fmt"
      "bytes"
      "github.com/aws/aws-sdk-go/aws"
      "github.com/aws/aws-sdk-go/aws/credentials"
      "github.com/aws/aws-sdk-go/aws/session"
      "github.com/aws/aws-sdk-go/service/s3"
)

func main() {
     const ACCESS_KEY_ID string = "NqTqcpjnNpCTW3bpMrcdN"
     const SECRET_ACCESS_KEY string = "XJx4sxLM9v3TsmKVdTwMJLhPFFxtrMhpJRNRqsgV"
     const BUCKET_NAME string = "go-example"
     const ENDPOINT string = "https://s3.pilw.io"
     const REGION string = "ee-east"

     nsession, err := session.NewSession(&aws.Config{
          Endpoint: aws.String(ENDPOINT),
          Credentials: credentials.NewStaticCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY, ""),
          Region: aws.String(REGION),
          LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
     })

     svc := s3.New(nsession)

     params := &s3.PutObjectInput{
          Bucket: aws.String(BUCKET_NAME),
          Key: aws.String("hello.txt"),
          ACL: aws.String("bucket-owner-full-control"),
          Body: bytes.NewReader([]byte("Hello world")),
     }

     resp, err := svc.PutObject(params)

     if err != nil {
        fmt.Println(err.Error())
        return
     }
 
     fmt.Println(resp)
}

 


PHP

Setup:

composer require aws/aws-sdk-php

 

<?php

require __DIR__.'/vendor/autoload.php';

use Aws\S3\S3Client;

define('ACCESS_KEY_ID', 'NqTqcpjnNpCTW3bpMrcdN');
define('SECRET_ACCESS_KEY', 'XJx4sxLM9v3TsmKVdTwMJLhPFFxtrMhpJRNRqsgV');
define('BUCKET_NAME', 'php-example');
define('ENDPOINT', 'https://s3.pilw.io');

$s3 = new S3Client([
        'region' => '',
        'version' => '2006-03-01',
        'endpoint' => ENDPOINT,
        'credentials' => [
               'key' => ACCESS_KEY_ID,
               'secret' => SECRET_ACCESS_KEY
        ]
]);

$result = $s3->putObject([
      'Bucket' => BUCKET_NAME,
      'Key' => 'hello.txt',
      'Body' => "Hello World!"
]);

echo PHP_EOL . 'object was successfully uploaded: ' . $result['ObjectURL'] . PHP_EOL;

 


Python

Setup:

pip3 install boto3

 

import boto3

ACCESS_KEY_ID = "NqTqcpjnNpCTW3bpMrcdN"
SECRET_ACCESS_KEY = "XJx4sxLM9v3TsmKVdTwMJLhPFFxtrMhpJRNRqsgV"
BUCKET_NAME = "python-example"
ENDPOINT = "https://s3.pilw.io"

s3 = boto3.resource(
       's3',
       aws_access_key_id = ACCESS_KEY_ID,
       aws_secret_access_key = SECRET_ACCESS_KEY,
       endpoint_url = ENDPOINT
)

s3.Object(BUCKET_NAME, 'hello.txt').put(Body="Hello world")

 


Ruby

Setup:

gem install aws-sdk-s3

 

require 'aws-sdk-s3'

ACCESS_KEY_ID = 'NqTqcpjnNpCTW3bpMrcdN'
SECRET_ACCESS_KEY = 'XJx4sxLM9v3TsmKVdTwMJLhPFFxtrMhpJRNRqsgV'
BUCKET_NAME = 'ruby-example'
ENDPOINT = 'https://s3.pilw.io'
REGION = 'ee-east'

Aws.config.update(
     endpoint: ENDPOINT,
     access_key_id: ACCESS_KEY_ID,
     secret_access_key: SECRET_ACCESS_KEY,
     region: REGION
)

s3 = Aws::S3::Client.new

s3.put_object(
     key: 'hello.txt',
     body: 'Hello world',
     bucket: BUCKET_NAME,
     content_type: 'text/plain'
)

 


Perl

Setup:

perl -MCPAN -e shell
install Amazon::S3

 

#!/usr/bin/perl
use warnings;
use strict;

use Amazon::S3;


my $access_key_id = "NqTqcpjnNpCTW3bpMrcdN";
my $secret_access_key = "XJx4sxLM9v3TsmKVdTwMJLhPFFxtrMhpJRNRqsgV";
my $bucket_name = "perl-example";
my $endpoint = "s3.pilw.io";

my $s3 = Amazon::S3->new(
         {   aws_access_key_id => $access_key_id,
             aws_secret_access_key => $secret_access_key,
             host => $endpoint,
             secure => 1,
             retry => 1
         }
);

my $bucket = $s3->bucket($bucket_name);

$bucket->add_key("hello.txt", "Hello world",
         { content_type => 'text/plain' }
);
Share on facebook
Share on linkedin
Share on twitter
Share on pinterest
Share on email

Related News