由于网络上对于S3接口方法的实现很少,所以写了关于S3包含文件分片上传、文件下载、生成预签名url、读取对象,分片读取、分别读取每个版本,保存到本地、取消用户当前授权,并重新授权、得到对象所有版本的ACL、得到对象的ACL、将某个对象授权给某个用户 此方法将作用于对象的所有版本、 将某个对象授权给某个用户 此方法仅作用于对象的当前版本
、为授权用户设置ACL、得到指定对象的版本、读取文件写到本地 在版本开启的情况下。如果不指定版本号,读取的是当前版本
、对象复制 、删除空目录、删除对象的各版本、按版本号删除 、列出对象各版本、删除对象元数据、修改对象元数据、得到对象的用户自定义元数据、检测对象是否存在,通过listobject方法进行检测、列出桶内所有对象、检测桶是否存在等方法
接口方法。AWS S3(对象存储)文件传输接口方法如下,需要提供原生接口方法手册,点个关注,留言或者加微信:lp1075553038:
package com.hitachivantara.example.hcp;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.BucketVersioningConfiguration;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.CanonicalGrantee;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.CopyObjectResult;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.DeleteObjectsResult;
import com.amazonaws.services.s3.model.DeleteVersionRequest;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.GetObjectMetadataRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.Grant;
import com.amazonaws.services.s3.model.GroupGrantee;
import com.amazonaws.services.s3.model.HeadBucketRequest;
import com.amazonaws.services.s3.model.HeadBucketResult;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ListVersionsRequest;
import com.amazonaws.services.s3.model.MultiObjectDeleteException;
import com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.Permission;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.s3.model.S3VersionSummary;
import com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest;
import com.amazonaws.services.s3.model.UploadPartRequest;
import com.amazonaws.services.s3.model.UploadPartResult;
import com.amazonaws.services.s3.model.VersionListing;
import com.hitachivantara.tools.BinaryUtils;
import com.hitachivantara.tools.Utils;
public class HS3Example {
//---1---修改成你的endpoint名称
private String endpointName = "t1.hcp.content.com";
//---2---修改为你的用户名和密码(如果拿到的已经是编码后的ak,sk。不需要使用Utils.getBase64Value和Utils.getMD5Value进行编码)
//private String ak = Utils.getBase64Value("admin");
//private String sk = Utils.getMD5Value("P@ssw0rd");
private String ak = "YWRtaW4=";
private String sk = "161ebd7d45089b3446ee4e0d86dbcf92";
// config
private ClientConfiguration myClientConfig = new ClientConfiguration();
// aws client
@SuppressWarnings("unused")
private AmazonS3 hs3Client = null;
public HS3Example() {
myClientConfig.setProtocol(Protocol.HTTP);
myClientConfig.setSignerOverride("S3SignerType");
hs3Client = AmazonS3ClientBuilder.standard()
.withClientConfiguration(myClientConfig)
.withPathStyleAccessEnabled(true)
.withEndpointConfiguration(new EndpointConfiguration(endpointName, ""))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ak, sk)))
.build();
}
public static void main(String[] args) throws UnsupportedEncodingException {
// 列出所有桶
//new HS3Example().listBuckets();
// 列出对象
//new HS3Example().listObjects("n1");
// 列出目录中对象
// new HS3Example().listObjectsByDir("n1","09");
// 列出对象,并分页
// new HS3Example().listObjectsByDirPagination("hks3test","abc/",1,"");
// new HS3Example().listObjectsByDirPagination("hks3test","abc/",2,"13/123/");
// 对象复制
// new HS3Example().copyObject("n1","123.docx","n2","123.docx");
// 检测桶是否存在
// new HS3Example().HeadBucketRequest("orclog");
// 创建桶
//new HS3Example().createBucket("n22");
// 创建对象
//new HS3Example().createObject("v83bniy1", "0123/dbar234.rar1", "c:\\test\\dbar.rar");
/*
* //读取对象 try { new HS3Example().getObjByEtag("n1", "0123/123.txt", dir+"1232.txt"); } catch (IOException
* e) { // TODO Auto-generated catch block e.printStackTrace(); }
*/
// new HS3Example().createObject("hks3test", "def/abc.txt", dir+"123.txt");
// 删除对象
new HS3Example().deleteObject("n99","abc.mp4");
// 批量删除对象
// new HS3Example().deleteObjects("hks3test","abc/def/123.zip","def/abc.txt");
// 删除桶,不支持清空桶
// new HS3Example().deleteBucket("b12");
/*- 读取对象
try {
//new HS3Example().getObject("testnamespace","树状结构 - 副本.xlsx",
//"C:/Users/lbin/Documents/HDSSvn/首都在线/testFile/树状结构 - 副本.xlsx");
new HS3Example().getObject("n99","abc.mp4",
"C:/test/abc.mp4");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
// 列出目录“北京/123/”下的对象
// new HS3Example().listObjectsByDir("5dnaywg0","1480426251603/blocks/2016-11-29-21-34-30/0/");
// 计算文件SHA256
// msha256md5Hashes(new File("C:\\Users\\lbin\\Documents\\HDSSvn\\首都在线\\testFile\\123.xls"));
// 读取文件写到本地
/*-
try {
//new HS3Example().getObject("testnamespace","树状结构 - 副本.xlsx",
//"C:/Users/lbin/Documents/HDSSvn/首都在线/testFile/树状结构 - 副本.xlsx");
new HS3Example().getObject("n1","1223.xls",
"C:/Users/lbin/Documents/HDSSvn/首都在线/testFile/test1111rr1.xls");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
/*
* try { new HS3Example().getObjectCache("n1","123.txt",
* "C:/Users/lbin/Documents/HDSSvn/首都在线/testFile/0ta2xtje.xls"); } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
*/
// 设置桶的ACL
// new HS3Example().setBucketACLForUser("testnamespace3");
// new HS3Example().setBucketACLForAuthenticatedUsers("testnamespace3");
// -new HS3Example().setBucketACLForPublic("jftyhvds");
// new HS3Example().setACLForBucketGroup("testnamespace3");
// 将某个对象授权给某个用户
/**/
// 为特定用户设置对象的ACL
//new HS3Example().setObjectACLForUser("n1","HCP-AW-VMDK-file1.nvram");
// new HS3Example().setObjectACLForUser_v("testnamespace3","test.xls");
// 为授权用户统一设置对象的ACL
// new HS3Example().setObjectACLForAuthenticatedUsers("testnamespace3","test.xls");
// 为匿名用户设置权限
// new HS3Example().setObjectACLForPublic("n1","0qwe/113.jpg");
// 得到对象的ACL
// -new HS3Example().getObjectACL("jftyhvds","1223.xls");
// new HS3Example().getObjectACL_v("testnamespace3","test.xls");