package com.example.cephs3;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.util.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* description
*
* @author ${user}
* @Time 2019-04-26
*/
public class TestS3UploadFile {
@Value("${s3.accessKey}")
// private String accessKey = "NB8SR8JU85V16ROBD4B3";
private String accessKey = "KE0AA98865C726GUWP6B";
// private String accessKey = "DQ2P7AIUZB956AMAE94Z";
@Value("${s3.secretKey}")
// private String secretKey = "gkIxvICALUahng0MTlSXhNafdN2FVpwM0qDBTtRB";
private String secretKey = "mbxbD5vZTrIZdMINhPGNtJocU2YjqFK66FLi0sV9";
// private String secretKey = "4CacXZRCKxjp3fssyIIX51Ai4xeQrQW7EpWwIuIs";
@Value("${s3.endPoint}")
// private String endPoint = "192.168.1.34:7480";
private String endPoint = "192.168.1.252:7480";
AmazonS3 connhandler = null;
private void init() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setProtocol(Protocol.HTTP);
connhandler = new AmazonS3Client(credentials, clientConfiguration);
connhandler.setEndpoint(endPoint);
}
@Test
public void TestlistBuckets() {
init();
List<Bucket> buckets = connhandler.listBuckets();
for( Bucket bucket: buckets) {
System.out.println( bucket.getName() + "\t" +
StringUtils.fromDate(bucket.getCreationDate()));
}
}
@Test
public void createBuckets() {
init();
if ( connhandler.doesBucketExist("bucket01") ) {
System.out.println("exits");
} else {
System.out.println("not exits");
}
//
// AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
// AmazonS3 conn = new AmazonS3Client(credentials);
//// com.amazonaws.services.s3.model.Region.US_Standard;
// conn.setEndpoint(endPoint);
// Bucket bucket = conn.createBucket("createbyapi");
Bucket bucket = connhandler.createBucket(new CreateBucketRequest("bucket01"));
// Bucket bucket = connhandler.createBucket("createbyapi");
// String bucketLocation = connhandler.getBucketLocation(new GetBucketLocationRequest("createbyapi"));
// System.out.println("bucket_name:" + bucket.getName());
// System.out.println("localtion:" + bucketLocation);
}
@Test
public void listObject() {
init();
//first_bucket
ObjectListing objectListing = connhandler.listObjects("bucket");
do {
for(S3ObjectSummary objectSummary: objectListing.getObjectSummaries()) {
System.out.println(objectSummary.getKey() + "\t" +
objectSummary.getSize() + "\t" +
StringUtils.fromDate(objectSummary.getLastModified()));
}
} while ( objectListing.isTruncated());
}
@Test
public void uploadFile() {
init();
// UploadPartRequest uploadPartRequest = new UploadPartRequest();
// conn.upload_file_multipart('/root/中国.mp4',u'videos/中国.mp4',bucket)
// uploadPartRequest.setBucketName("first_bucket");
//
// connhandler.uploadPart(uploadPartRequest);
// PutObjectResult putObjectResult = connhandler.putObject("first_bucket", accessKey, new File("F:\\20190101\\abc.txt"));
PutObjectResult putObjectResult =
connhandler.putObject("bucket1",
"20170301/58434_128M.txt",
new File("F:\\tmpdata\\20170301\\Z_SURF_I_58434_20170301000000_O_AWS_DAY.txt"));
// connhandler.put
System.out.println(putObjectResult.getContentMd5() + "\t" + putObjectResult.getETag());
}
@Test
public void getObject() throws IOException {
init();
S3Object s3Object = connhandler.getObject(new GetObjectRequest("first_bucket", "20190101/abc.txt"));
System.out.println("content-type: " + s3Object.getObjectMetadata().getContentType());
System.out.println("Contect: ");
InputStream inputStream = s3Object.getObjectContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ( (line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println();
}
@Test
public void DeleteBucket() {
init();
connhandler.deleteBucket("createbucketbyapi");
}
@Test
public void TestACL() {
init();
AccessControlList accessControlList = connhandler.getObjectAcl("first_bucket", "20190101/abc.txt");
System.out.println(accessControlList.getOwner().getDisplayName());
System.out.println(accessControlList.getOwner().getId());
}
@Test
public void downloadObject() {
init();
connhandler.getObject(new GetObjectRequest("first_bucket", "20190101/abc.txt"),
new File("F:\\20190101\\download.txt"));
}
@Test
public void deleteObject() {
init();
connhandler.deleteObject("first_bucket", "NB8SR8JU85V16ROBD4B3");
}
@Test
public void TestURLRequest() {
// 类似于生成的url是在有限时间(比如一个小时内)内生效的,超过时间范围就失效了
init();
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest("bucket1", "20170111/Z_SURF_I_53983_20170111000000_O_AWS_DAY.txt");
System.out.println(connhandler.generatePresignedUrl(generatePresignedUrlRequest));
}
@Test
public void TestUrl12Hour() {
init();
Date expiration = new Date();
long expirationMS = expiration.getTime();
expirationMS += 1000 * 60 * 60 * 12;
expiration.setTime(expirationMS);
GeneratePresignedUrlRequest request =
new GeneratePresignedUrlRequest("first_bucket", "20190101/abc.txt");
request.withMethod(HttpMethod.PUT);
request.withExpiration(expiration);
URL url = connhandler.generatePresignedUrl(request);
System.out.println(url);
}
@Test
public void TestDeleteObjects() {
String[] object_keys = new String[]{"", ""};
List<String> objectListKeys = null;
objectListKeys = new ArrayList<String>();
init();
DeleteObjectsRequest dor = new DeleteObjectsRequest("first_bucket");
dor.withKeys();
connhandler.deleteObjects(dor);
}
@Test
public void CopyObject() {
init();
connhandler.copyObject("first_bucket", "20190101/abc.txt",
"bucket", "20190101/abc1.txt" );
}
@Test
public void TestList() {
String[] strlst = new String[]{"abc", "aef"};
String[] strlst1 = new String[]{""};
boolean flag = org.springframework.util.StringUtils.isEmpty(strlst1);
System.out.println(flag);
}
// 验证一个用户可以创建多少bucket
@Test
public void TestCreateBuckets() {
}
// 每个bucket可以创建多少object
@Test
public void TestCreateObjects() {
}
}