AWS SDK 访问阿里云 OSS

3 篇文章 0 订阅

S3 已经成为云对象存储领域的规范,主流的对象存储都有对它的支持。阿里云 OSS 也支持 S3 协议,我们可以使用AWS的SDK对其进行操作,当然由于OSS与S3在功能和实现上的差别,OSS 不可能支持所有的AWS S3操作,但是,对于日常大部分操作,它都是支持的。

##AWS CLI

  • aws configure --p aliyun
  • aws configure set s3.addressing_style virtual --p aliyun
  • aws s3 ls --endpoint-url http://oss-cn-hangzhou.aliyuncs.com --p aliyun

##AWS JAVA SDK

  • 获取AWS JAVA SDK(maven)
        <dependency>
    		<groupId>com.amazonaws</groupId>
    		<artifactId>aws-java-sdk</artifactId>
    		<version>1.11.298</version>
    	</dependency>
    
  • 示例代码
         import java.util.List;
    
         import com.amazonaws.auth.AWSCredentials;
         import com.amazonaws.auth.AWSStaticCredentialsProvider;
         import com.amazonaws.auth.profile.ProfileCredentialsProvider;
         import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
         import com.amazonaws.services.s3.AmazonS3;
         import com.amazonaws.services.s3.AmazonS3ClientBuilder;
         import com.amazonaws.services.s3.model.Bucket;
    
         public class AwsAliyun {
              public static void main(String[] args) {
    	           AWSCredentials credentials = new ProfileCredentialsProvider("aliyun").getCredentials();
    
    	           AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
    			.withCredentials(new AWSStaticCredentialsProvider(credentials))
    			                 .withEndpointConfiguration(new EndpointConfiguration("http://oss-cn-hangzhou.aliyuncs.com", "oss")).build();
    
    	           List<Bucket> lb = s3Client.listBuckets();
    	           for(Bucket b: lb) {
    		           System.out.println(b.getName());
    	           }
             }
        }
    

##AWS PYTHON SDK

  • 获取AWS PYTHON SDK
    pip install boto3
    
  • 示例代码
        #!/usr/bin/env python
        #coding: utf-8
    
        import boto3
    
        session = boto3.session.Session(profile_name='aliyun')
        s3 = session.resource('s3', endpoint_url='http://oss-cn-hangzhou.aliyuncs.com')
    
        for bucket in s3.buckets.all():
            print(bucket.name)
    

##AWS GO SDK

  • 获取AWS GO SDK
     
         package main
    
         import (
              "fmt"
    
              "github.com/aws/aws-sdk-go/aws"
              "github.com/aws/aws-sdk-go/aws/session"
              "github.com/aws/aws-sdk-go/service/s3"
         )
    
         func main() {
              sess := session.Must(session.NewSessionWithOptions(session.Options{
              Config: aws.Config{
                  Endpoint: aws.String("http://oss-cn-hangzhou.aliyuncs.com"), 
                  Region: aws.String("oss")},
              Profile: "aliyun",
         }))
    
         svc := s3.New(sess)
         resp, _:= svc.ListBuckets(&s3.ListBucketsInput{})
         for _, bucket := range resp.Buckets {
             fmt.Println(*bucket.Name)
         }
    

}
```

##AWS PHP SDK

  • 获取AWS PHP SDK
        curl -sS https://getcomposer.org/installer | php
        php composer.phar require aws/aws-sdk-php
    
  • 示例代码
    require 'vendor/autoload.php';
    use Aws\S3\S3Client;
    
    $client = S3Client::factory([  
        'version' => '2006-03-01',
        'profile' => 'aliyun',
        'region' => 'oss-cn-hangzhou',
        'endpoint' => 'http://oss-cn-hangzhou.aliyuncs.com'
    ]); 
    
    $result = $client->listBuckets();    
    foreach($result['Buckets'] as $b) {    
        var_dump($b);    
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值