Amazonaws S3 java SDK连接初探

Posted 李肖峰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Amazonaws S3 java SDK连接初探相关的知识,希望对你有一定的参考价值。

package com.inslink.sinosoft.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3Sample {


    public static void main(String[] args) throws IOException {
        
            //创建Amazon S3对象使用明确凭证
        BasicAWSCredentials credentials = new BasicAWSCredentials("your accesskey", "your secretkey");
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setSignerOverride("S3SignerType");//凭证验证方式
        clientConfig.setProtocol(Protocol.HTTP);//访问协议
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
           .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withClientConfiguration(clientConfig)
                    .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration(//设置要用于请求的端点配置(服务端点和签名区域)
                           "s3.yiducloud.cn",
                           "cn-north-1")).withPathStyleAccessEnabled(true)//是否使用路径方式,是的话s3.yiducloud.cn/bucketname
                .build();

            System.out.println("Uploading a new object to S3 from a file
");
            
            //枚举bucket
            List<Bucket> buckets = s3Client.listBuckets();
            for (Bucket bucket : buckets) {
                System.out.println("Bucket: " + bucket.getName());
            }
            //枚举bucket下对象
            ObjectListing objects = s3Client.listObjects("sinosoft-ocr-bucket");
            do {
                for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                    System.out.println("Object: " + objectSummary.getKey());
                }
                objects = s3Client.listNextBatchOfObjects(objects);
            } while (objects.isTruncated());
            
            
            //文件上传
            try {
                    s3Client.putObject("bucketname", "keyname", new File("your file path"));
            } catch (AmazonServiceException e) {
                System.err.println(e.getErrorMessage());
                System.exit(1);
            }
            
            //文件下载
            try {
                S3Object o = s3Client.getObject("bucketname", "your file‘s keyname");
                S3ObjectInputStream s3is = o.getObjectContent();
                FileOutputStream fos = new FileOutputStream(new File("your save file path"));
                byte[] read_buf = new byte[1024];
                int read_len = 0;
                while ((read_len = s3is.read(read_buf)) > 0) {
                    fos.write(read_buf, 0, read_len);
                }
                s3is.close();
                fos.close();
            } catch (AmazonServiceException e) {
                System.err.println(e.getErrorMessage());
                System.exit(1);
            } catch (FileNotFoundException e) {
                System.err.println(e.getMessage());
                System.exit(1);
            } catch (IOException e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
            
            //其他操作方法参考:https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/examples-s3-objects.html
            //或者下载官网源码
            


    }

}

 

以上是关于Amazonaws S3 java SDK连接初探的主要内容,如果未能解决你的问题,请参考以下文章

导入com.amazonaws.services.s3.AmazonS3ClientBuilder无法解析

Amazon S3 上传问题 Android SDK、com.amazonaws.AmazonClientException:读取的数据 (4567265) 多于预期 (4561427)

“无法执行 HTTP 请求:连接到 <bucket-name>.s3.amazonaws.com:443 失败:连接超时

使用 aws-java-sdk-s3 时出错

为什么这个错误?连接到sts.amazonaws.com:443 [sts.amazonaws.com/54.239.29.25]失败:连接超时:com.amazonaws.SdkClientExce

AWS java sdk 1.10.2 中不存在包 com.amazonaws.services.lambda.runtime