对象存储

产品文档

对象存储

下载

使用getObject方法,并在请求中提供存储空间名称和对象名称。

下载方式

下载整个Object


String bucket_name = "<your bucketname>";
String key = "your keyname";
System.out.format("Downloading %s from S3 bucket %s...\n", key, bucket_name);

try {
    S3Object o = s3.getObject(bucket_name, key);
    S3ObjectInputStream s3is = o.getObjectContent();
    FileOutputStream fos = new FileOutputStream(new File(key));
    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);
}
    System.out.println("Done!");  

范围下载


       
String bucket_name = "<your bucketname>";
String key = "<your keyname>";
String file_path = "<your path>";
int start = 1;
int end = 1000;
	    
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket_name, key).withRange(start,end);
s3.getObject(getObjectRequest, new File(file_path));
更新时间:2019-06-26 19:28:23