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