Sử dụng SDK để truy cập dữ liệu#

Đối với nhà phát triền phần mềm, cần có bộ công cụ để tiện lợi trong quá trình phát triển. Sun S3 tương thích với các S3 SDK sẵn có. Bài viết sau sẽ hướng dẫn setup code để dùng SDK cho các ngôn ngữ khác nhau.

Mục tiêu#

  • Sử dụng SDK của các ngôn ngữ khác nhau để integrate với Sun-S3

Java#

Setup

Maven

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.18.28</version>
</dependency>

Graddle

testImplementation group: 'software.amazon.awssdk', name: 's3', version: '2.18.28'

Create connection

AwsCredentials credentials = AwsBasicCredentials.create("<accessKey>", "<secretKey>");
AwsCredentialsProvider p = () -> credentials;
S3Configuration s3 = S3Configuration.builder()
    .pathStyleAccessEnabled(true)
    .build();
S3Client conn = S3Client.builder()
    .credentialsProvider(p)
    .endpointOverride(URI.create("https://s3.sunteco.app"))
    .serviceConfiguration(s3).build();

JavaScript / Node.js / ReactJs/ TypeScript#

Setup

Prerequisites: npm

npm install aws-sdk

Create connection

const s3 = new AWS.S3({
  endpoint: "https://s3.sunteco.app",
  credentials: {
    accessKeyId: "<accessKey>",
    secretAccessKey: "<secretKey>",
  },
  s3ForcePathStyle: true,
});

Python#

Setup

pip install boto

Create connection

import boto
import boto.s3.connection
access_key = '<accessKey>'
secret_key = '<secretKey>'
 
conn = boto.connect_s3(
        aws_access_key_id = access_key,
        aws_secret_access_key = secret_key,
        host = 's3.sunteco.app',
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
        )

hoặc boto3

import os
import sys
import json
 
import boto3
 
def main():
 
    s3 = boto3.client('s3',
                      endpoint_url="https://s3.sunteco.app",
                      aws_access_key_id="<Access key>",
                      aws_secret_access_key="<Secret key>")
 
    response = s3.list_buckets()
    for item in response['Buckets']:
        print(item['CreationDate'], item['Name'])
 
if __name__ == '__main__':
    main()

CSHARP#

Prerequisites: dotnet core

Setup

dotnet add package AWSSDK.S3

Create connection

AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = "https://s3.sunteco.app";
 
AmazonS3Client s3Client = new AmazonS3Client(
        <accessKey>,
        <secretKey>,
        config
        );