兼容工具

兼容说明

大多数基于AWS S3开发的工具都可以设置访问地址。通过设置这些工具的访问地址为BOS的AWS S3服务域名,您就可以使用这些工具访问BOS。下面以一些常用SDK和工具为例,说明如何对接BOS。

说明:

字串 含义
$ACCESS_KEY 百度智能云账号的Access key
$SECRET_KEY 百度智能云账号的Secret key

AWS SDK for Python

  1. 安装Boto类库:

    pip install boto3
    
  2. 使用AWS SDK for python访问Bos

    import boto3
    from botocore.client import Config
    s3 = boto3.client(
            's3',
            aws_access_key_id=$ACCESS_KEY,
            aws_secret_access_key=$SECRET_KEY,
            endpoint_url='s3.bj.bcebos.com',
            region_name='bj',
            config = Config(
                signature_version='s3v4',
            )
         )
    # Use S3 client
    s3.create_bucket(...)
    

AWS SDK for Java

  1. 添加依赖包到pom.xml

    //添加下面AWS Java SDK依赖包到pom.xml
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk</artifactId>
      <version>1.11.82</version>
    </dependency>
    
  2. 使用AWS SDK for java访问Bos

    import java.io.IOException;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3Client;
    import com.amazonaws.services.s3.model.*;
    import com.amazonaws.services.s3.S3ClientOptions;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.SDKGlobalConfiguration;
    
    public class S3Sample {
        public static void main(String[] args) throws IOException {
            System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
            AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials($ACCESS_KEY,$SECRET_KEY));
            s3.setEndpoint("s3.bj.bcebos.com");
            S3ClientOptions options = new S3ClientOptions();
            options.withChunkedEncodingDisabled(true); // Must to have
            s3.setS3ClientOptions(options);
    
            // Use S3 Client
            s3.createBucket(...);
        }
    
    }
    
  3. 编译代码

    mvn package
    

AWS PHP SDK

  1. 安装:下载aws.phar,更多安装方式见AWS PHP SDK安装方式
  2. 使用AWS SDK for PHP访问Bos

    <?php    
    require 'aws.phar';                  
    use Aws\S3\S3Client;
    use Aws\Exception\AwsException;     
    
    $s3Client = new S3Client([          
        'version'     => 'latest',
        'region'      => 'bj',
        'credentials' => [
            'key'    => $ACCESS_KEY,
            'secret' => $SECRET_KEY,
        ],
        'endpoint' => 'bj.s3.bcebos.com',
        'signature_version' => 'v4',
    ]);
    
    $buckets = $s3Client->listBuckets();
    foreach ($buckets['Buckets'] as $bucket){
        echo $bucket['Name']."\n";
    }
    

AWS Golang SDK

  1. 安装

    go get -u github.com/aws/aws-sdk-go
    
  2. 使用AWS SDK for Golang访问Bos

    import (
        "github.com/aws/aws-sdk-go/aws"                                            
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/s3"
        "github.com/aws/aws-sdk-go/aws/credentials"
    )
    conf := &aws.Config{
            Region:           aws.String("bj"),
            Endpoint:         aws.String("bj.s3.bcebos.com"),
            Credentials:      credentials.NewStaticCredentials($ACCESS_KEY, $SECRET_KEY,, ""),
        }
    sess := session.Must(session.NewSessionWithOptions(session.Options{Config:*conf}))
    svc := s3.New(sess)
    getObjectParams := &s3.GetObjectInput{
            Bucket:             aws.String("my-bucket"), 
            Key:                aws.String("my-object"), 
    }
    getObjectResp, err := svc.GetObject(getObjectParams)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    

AWS CLI工具

  1. 安装AWS CLI工具

    pip install awscli
    
  2. 使用AWS CLI访问BOS

    • 编辑配置文件
    $ cat ~/.aws/config
    [default]
    s3 =
        signature_version = s3v4
    region = bj
    
    $ cat ~/.aws/credentials
    [default]
    aws_access_key_id = $ACCESS_KEY
    aws_secret_access_key = $SECRET_KEY
    
    • 执行命令
    aws --endpoint-url http://s3.bj.bcebos.c