上传对象

您可以使用 putObject 接口上传对象,以下代码展示如何上传一个对象:

文件上传

String bucketName = "<your-bucket-name>";
String objectKey = "<your-object-key>";
String filePath = "<your-file-path>";
try {
    s3.putObject(bucketName, objectKey, new File(filePath));
} catch (AmazonServiceException e) {
    System.err.println(e.getErrorMessage());
}

流式上传

String bucketName = "<your-bucket-name>";
String objectKey = "<your-object-key>";
String filePath = "<your-file-path>";
try {
    FileInputStream is = new FileInputStream(new File(filePath));
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentLength(is.available());
    
    PutObjectRequest req = new PutObjectRequest(bucketName, objectKey, is, meta);
    s3Client.putObject(req);
} catch (AmazonServiceException e) {
    System.err.println(e.getErrorMessage());
} catch (Exception e) {
    e.printStackTrace();
}

您可以在上传对象时设置对象元数据。对象可设置的元数据如下表:

Java-sdk 方法 作用
ObjectMetadata.setContentType 设置HTTP/HTTPS请求头部中的 Content-Type
ObjectMetadata.setContentLanguage 设置HTTP/HTTPS请求头部中的 Content-Language
ObjectMetadata.setCacheControl 设置HTTP/HTTPS请求头部中的 Cache-Control
ObjectMetadata.setContentDisposition 设置HTTP/HTTPS请求头部中的 Content-Disposition
ObjectMetadata.setContentEncoding 设置HTTP/HTTPS请求头部中的 Content-Encoding
ObjectMetadata.setContentLength 设置HTTP/HTTPS请求头部中的 Content-Length,设置请求body的长度(单位:字节)
ObjectMetadata.setContentMD5 对象数据的MD5值(经过 Base64编码),提供给服务端校验数据完整性。

您可以在 ObjectMetadata 中设置对象的元数据,并在 PutObjectRequest 加入此设置。以下代码展示如何设置对象元数据:

String bucketName = "<your-bucket-name>";
String objectKey = "<your-object-key>";
String filePath = "<your-file-path>";
FileInputStream fis;
try {
    fis = new FileInputStream(new File(filePath));
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return;
}
ObjectMetadata metadata = new ObjectMetadata();
try {
    metadata.setContentLength(fis.available());//在请求头部header的Content-Length属性设置文件长度
} catch (IOException e) {
    e.printStackTrace();
}
try {
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, fis, metadata);
    PutObjectResult result = s3.putObject(putObjectRequest);
    System.out.println(result.getETag());
} catch (AmazonServiceException e) {
    System.err.println(e.getErrorMessage());
}

上传对象时如果不设置对象访问权限,默认的对象访问权限是 private,即只有对象的拥有者才能访问该对象,若需要改为公共读 public read,可以在上传对象时的PutObjectRequest 对象中加入对象访问权限acl设置,您也可以在上传对象以后通过 setObjectAcl 更改对象访问权限,具体对象访问权限设置请参考3.2.8节。

以下代码展示如何设置在上传对象时设置对象访问权限为公共读:

String bucketName = "<your-bucket-name>";
String objectKey = "<your-object-key>";
String filePath = "<your-file-path>";
FileInputStream fis;
try {
    fis = new FileInputStream(new File(filePath));
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return;
}
ObjectMetadata metadata = new ObjectMetadata();
try {
    metadata.setContentLength(fis.available());//在请求头部header的Content-Length属性设置文件长度
} catch (IOException e) {
    e.printStackTrace();
}
try {
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, fis, metadata);
    putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);//设置上传对象的Acl为公共读,私有写
    PutObjectResult result = s3.putObject(putObjectRequest);
    System.out.println(result.getETag());
} catch (AmazonServiceException e) {
    System.err.println(e.getErrorMessage());
}

注意:putObject对文件大小有限制,最大能上传1GB大小的文件,超过1GB需要使用分片上传。