Object接口

Put Object

兼容说明

  • x-amz-tagging 不支持
  • x-amz-website-redirect-location 不支持
  • Expires 不支持
  • Content-Encoding 不支持
  • Java SDK不支持http上传

代码示例

Java:

String content = "test put object content";
String testKey = "testKey";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,testKey,"/");
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(content.length());
Map<String,String> userMeta = new TreeMap<>();
userMeta.put("userMeta1","meta1");
userMeta.put("userMeta2","meta2");
objectMetadata.setUserMetadata(userMeta);
putObjectRequest.setMetadata(objectMetadata);
putObjectRequest.setInputStream(new ByteInputStream(content.getBytes(Charset.forName("UTF-8")),content.getBytes(Charset.forName("UTF-8")).length));
s3Client.putObject(putObjectRequest);

Put Object Copy

兼容说明

  • x-amz-metadata-directive,不支持设置,用户原信息会复制过去
  • x-amz-copy-xxx 等参数不支持
  • x-amz-storage-class,存储级别使用默认(STANDARD)的就好,NOS不支持其他的存储级别
  • x-amz-tagging-directive,x-amz-website-redirect-location 也不支持

代码示例

Java:

CopyObjectResult copyObjectResult = s3Client.copyObject(srcBucket,srcKey,destBucket,destKey);
System.out.println(copyObjectResult.getETag());
System.out.println(copyObjectResult.getLastModifiedDate());

Get Object

兼容说明

  • NOS 不支持的操作,相关的响应头是不会返回的
  • If-Unmodified-Since,If-Match,If-None-Match 不支持

代码示例

Java:

String testKey = "testKey";
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName,testKey);
S3Object s3Object = s3Client.getObject(getObjectRequest);
System.out.println(s3Object.getObjectMetadata().getContentLength());
if(s3Object != null){
   S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
   byte[] buf = new byte[1024];
   int readLen;
   try {
       while((readLen = s3ObjectInputStream.read(buf)) > -1){
           System.out.println(new String(buf,0,readLen));
       }
       System.out.println(s3Object.getObjectMetadata().getUserMetadata());
   } catch (IOException e) {
       e.printStackTrace();
   }
}

Delete Object

兼容说明

  • NOS 会直接删除对象
  • 删除对象只需要正确的 ak/sk
  • 返回值中不会包含 x-amz-delete-marker和x-amz-version-id

代码示例

Java:

s3Client.deleteObject(bucketName,testKey);

Delete Multiple Objects

兼容说明

接口和 S3 兼容

代码示例

Java:

String testBucketName  = bucketName;
String testKey = "test";
for(int i = 0;i < 5;++i){
    s3Client.putObject(testBucketName,testKey + i,"test" + i);
}
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(testBucketName);
List<DeleteObjectsRequest.KeyVersion> list = new LinkedList<>();
for(int i = 0; i < 3;++i){
    list.add(new DeleteObjectsRequest.KeyVersion(testKey + i));
}
deleteObjectsRequest.setKeys(list);
deleteObjectsRequest.setQuiet(true);
DeleteObjectsResult deleteObjectsResult = s3Client.deleteObjects(deleteObjectsRequest);
for(DeleteObjectsResult.DeletedObject deleteObject : deleteObjectsResult.getDeletedObjects()){
    System.out.println(deleteObject.getKey());
}

Head Object

兼容说明

  • 和 GetObject 一样,也不支持 If-Unmodified-Since,If-Match,If-None-Match 参数
  • 同样的,加密也不支持
  • 返回中带有 Object 该有的信息

代码示例

Java:

ObjectMetadata objectMetadata =  s3Client.getObjectMetadata(bucketName,testKey);
System.out.println(objectMetadata.getUserMetadata());
System.out.println(objectMetadata.getContentLength());
System.out.println(objectMetadata.getETag());

List Objects

兼容说明

S3 的List Objects 有两个版本(v1,v2),NOS 目前只兼容 v1

代码示例

Java:

ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
listObjectsRequest.setBucketName("sdktest-private");
List<S3ObjectSummary> listResult = new ArrayList<>();
ObjectListing listObjects = s3Client.listObjects(listObjectsRequest);
do {
    listResult.addAll(listObjects.getObjectSummaries());
    if (listObjects.isTruncated()) {
        ListObjectsRequest request = new ListObjectsRequest();
        request.setBucketName(listObjectsRequest.getBucketName());
        request.setMarker(listObjects.getNextMarker());
        listObjects =  s3Client.listObjects(request);
    } else {
        break;
    }
} while (listObjects != null);