Amazonaws S3 java SDK串連初探

來源:互聯網
上載者:User

標籤:exception   protocol   string   源碼   package   ack   model   port   puts   

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\n");                        //枚舉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串連初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.