package .plugins.s3_step;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .s3.AmazonS3;
import .s3.AmazonS3Client;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import .;
import ..S3Object;
import ..S3ObjectInputStream;
import ..S3ObjectSummary;
import .;
import .;
public class Demo {
AWSCredentials credentials = null;
public AmazonS3 getS3Client(String access_key, String secret_key,
String endpoint) {
ClientConfiguration conf = null;
if (conf == null) {
conf = new ClientConfiguration();
credentials = new BasicAWSCredentials(access_key, secret_key);
}
AmazonS3 s3 = new AmazonS3Client(credentials, conf);
((Regions.CN_NORTH_1));
if (endpoint != null)
(endpoint);
return s3;
}
// ============================ bucket ================================
public void testCreateBucket(AmazonS3 s3Client, String bucketName) {
("======================创建 Bucket==========================");
Bucket bucket = (bucketName);
(bucket);
("创建 Bucket : " + ());
("======================创建 Bucket==========================");
}
public void testDeleteBucket(AmazonS3 s3Client, String bucketName) {
("======================删除 Bucket==========================");
("删除 Bucket : " + bucketName);
(bucketName);
("======================删除 Bucket==========================");
}
public void testDoesBucketExist(AmazonS3 s3Client, String bucketName) {
("======================Bucket 是否存在==========================");
boolean bucketExist = (bucketName);
(("%s : %s", bucketName, bucketExist));
("======================Bucket 是否存在==========================");
}
public void testListBuckets(AmazonS3 s3Client) {
("======================枚举 Buckets==========================");
List<Bucket> buckets = ();
for (Bucket bucket : buckets) {
(("%s - %s - %s", (), (), ()));
}
("======================枚举 Buckets==========================");
}
public void testListObjects(AmazonS3 s3Client, String bucketName) {
("======================枚举 Objects==========================");
List<String> objectKeys = new ArrayList<String>();
String prefix = "/s3demo/test/";
// for (int i = 0; i < 5; i++) {
// (prefix + i);
// }
// for (String objectKey : objectKeys) {
// testPutObject(s3Client, bucketName, objectKey);
// }
ObjectListing objects = (bucketName);
List<S3ObjectSummary> s3Objects = ();
for (S3ObjectSummary s3os : s3Objects) {
(("%s - %s - %s", (), (), ()));
}
// prefix
ListObjectsRequest listObjReq = new ListObjectsRequest();
(bucketName);
("/");
(prefix);
ObjectListing preobjects = (listObjReq);
List<String> preobjs = ();
for (String preobj : preobjs) {
(preobj + "\n");
}
("======================枚举 Objects==========================");
}
// ============================ bucket ================================
// ============================ object ================================
public void testPutObject(AmazonS3 s3Client, String bucketName, String objectKey,String file_source_address) {
("======================上传 object==========================");
(new PutObjectRequest(bucketName, objectKey, new File(file_source_address)).withCannedAcl());
("======================上传 object==========================");
}
public void testGetObject(AmazonS3 s3Client, String bucketName,String objectKey) throws Exception {
("======================下载 object==========================");
try {
S3Object s3Object = (new GetObjectRequest(bucketName,objectKey));
//下载文件到指定地方
InputStream reader = new BufferedInputStream(());
File file = new File("localFilename");
FileOutputStream fos = new FileOutputStream(file);
OutputStream writer = new BufferedOutputStream(fos);
int read = -1;
while ( ( read = () ) != -1 ) {
(read);
}
();
();
();
//获取一个request,生成一个下载的url地址,可以在浏览器上下载
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);
//生成公用的url
URL url = (urlRequest);
("=========URL=================" + url + "============URL=============");
if (url == null) {
throw new Exception("can't get s3 file url!");
}
("URL=" + ());
} catch (Exception e) {
();
}
("======================下载 object==========================");
}
public void testDeleteObject(AmazonS3 s3Client, String bucketName,String objectKey) {
("======================删除 object==========================");
(bucketName, objectKey);
("======================删除 object==========================");
}
public void testCopyObject(AmazonS3 s3Client, String srcBucketName, String srcObjectKey, String dstBucketName, String dstObjectKey) {
("======================复制 object==========================");
CopyObjectResult cpres = (srcBucketName, srcObjectKey, dstBucketName, dstObjectKey);
String text = ("test copy objcet %s from %s to %s.%s", srcObjectKey, srcBucketName, dstBucketName, dstObjectKey);
(text);
("object etag : " + ());
("======================复制 objects==========================");
}
public void testMultiUpload(AmazonS3 s3Client, String bucketName, String objectKey) {
("======================分块上传==========================");
("初始化分块上传");
InitiateMultipartUploadRequest initMultiUp = new InitiateMultipartUploadRequest(bucketName, objectKey);
InitiateMultipartUploadResult initMultiUpRes = (initMultiUp);
String uploadId = ();
("分块上传 MultiUpload Id : " + uploadId);
("上传块");
List<PartETag> partETags = new ArrayList<PartETag>();
for (int i = 1; i <= 3; i++) {
UploadPartRequest uploadPartReq = new UploadPartRequest();
(bucketName);
(objectKey);
(uploadId);
(i);
byte[] part = new byte[1024 * 1024 * 6]; // 分块上传一个块必须大于等于5M
if (i == 3) {
(true);
part = new byte[1024 * 1024 * 3]; // 最后一个块可以小于5M
}
SecureRandom random = new SecureRandom();
(part);
("块大小 = " + );
();
InputStream inputStream = new ByteArrayInputStream(part);
(inputStream);
UploadPartResult uploadPartRes = (uploadPartReq);
PartETag partETag = ();
(partETag);
(("上传块 %s - %s", (), ()));
}
CompleteMultipartUploadRequest comMultiReq = new CompleteMultipartUploadRequest(
bucketName, objectKey, uploadId, partETags);
CompleteMultipartUploadResult comMultires = (comMultiReq);
("完成上传 etag : " + ());
("测试终止上传");
String objectKey1 = "test_abort_multi_upload";
InitiateMultipartUploadRequest initMultiUp1 = new InitiateMultipartUploadRequest(
bucketName, objectKey1);
InitiateMultipartUploadResult initMultiUpRes1 = s3Client
.initiateMultipartUpload(initMultiUp1);
String uploadId1 = ();
("终止上传 upload id : " + uploadId1);
AbortMultipartUploadRequest abortMultiUp = new AbortMultipartUploadRequest(bucketName, objectKey1, uploadId1);
(abortMultiUp);
("======================分块上传==========================");
}
public void testListUploadIds(AmazonS3 s3Client, String bucketName) {
("======================枚举分块上传uploadId==========================");
String keyPrefix = "/demo/listupids";
for (int i = 1; i < 5; i++) {
String objectKey = keyPrefix + i;
InitiateMultipartUploadRequest initMultiUp = new InitiateMultipartUploadRequest(bucketName, objectKey);
(initMultiUp);
}
ListMultipartUploadsRequest listMultiUpReq = new ListMultipartUploadsRequest(bucketName);
MultipartUploadListing multiUploads = (listMultiUpReq);
List<MultipartUpload> uploads = ();
for (MultipartUpload up : uploads) {
(("uploadId : %s - key : %s", (), ()));
}
("======================枚举分块上传uploadId==========================");
}
public void testListMultiParts(AmazonS3 s3Client, String bucketName, String objectkey, String uploadId) {
("======================枚举分块上传part==========================");
ListPartsRequest listPartsReq = new ListPartsRequest(bucketName, objectkey, uploadId);
PartListing parts = (listPartsReq);
List<PartSummary> ps = ();
for (PartSummary p : ps) {
(("partNum : %s - partEtag : %s", (), ()));
}
("======================枚举分块上传part==========================");
}
public void testIsFileExists(AmazonS3 s3Client, String s3Path,String keyName){
("======================判断文件是否已经存在==========================");
try{
boolean flag = false;
S3Object s3Object = (s3Path, keyName);
if(s3Object!=null){
flag = true;
}
if(flag){
("File exists,filename:"+keyName);
}
}catch(Exception e){
();
}
}
// ============================ object ================================
public static void main(String[] args) throws IOException {
String access_key = "s3的access key";
String secret_key = "s3的secret key";
String endpoint = "s3的地址";
Demo d = new Demo();
AmazonS3 s3Client = d.getS3Client(access_key, secret_key, endpoint);
String bucketName = "demo";
String bucketName1 = "demo1";
//创建bucket
// (s3Client, bucketName);
//查询bucket列表
// (s3Client);
//判断bucket是否存在
// (s3Client, bucketName);
//删除bucket
// (s3Client, bucketName2);
//上传文件
// String objectKey = "var/lib/";
// String objectKey1 = "var/lib/";
// String file_source_address = "E:/demo/test1/";
// (s3Client, bucketName, objectKey,file_source_address);
//下载文件
// (s3Client, bucketName, objectKey);
// //复制文件
// (s3Client, bucketName, objectKey, bucketName1, objectKey1);
//bucket下面文件列表
// (s3Client, bucketName);
//判断文件是否存在
// (s3Client, bucketName, objectKey);
}
}