package com.supServer.common.utils;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.*;

import java.io.File;


public class TianYiUtil {
	
	// OOS_ACCESS_ID
	private static String accessKey = "L6pr75HLgfpCW2ULNSq9";
	
	// OOS_ACCESS_KEY
	private static String secretAccessKey = "cTNvyScRuCwc03zXlv3ddoec45RwsBIAQhNhDw5m";
	
	// OOS_ENDPOINT
	private static String endpoint = "cq4oss.ctyunxs.cn";
	
	//xxx 域名？
	//private static String url = "credit-report.cq4oss.ctyunxs.cn";
	
	
	public static void main(String[] args) {
		
		//CreateAccessKeyResult createAccessKeyResult = new CreateAccessKeyResult();
		
		AmazonS3 client = getAmazonS3();
//		Bucket bucket = client.createBucket("cqzx");
//		System.out.println(bucket.getCreationDate());
//		System.out.println(bucket.getName());
//		System.out.println(bucket.getOwner());
//		AccessControlList list = client.getBucketAcl("mytest-bucket");
//		System.out.println(list);
		//AccessControlList [owner=S3Owner [name=bssUser100000001263,id=bssUser100000001263], grants=[Grant [grantee=com.amazonaws.services.s3.model.CanonicalGrantee@936d57aa, permission=FULL_CONTROL]]]
		//putObject(client);
//
//		boolean existBucket = isExistBucket(client, "cqzx");
//		System.out.println(existBucket);
	}
	
	/**
	 * 创建bucket（空间名）
	 * @methodName createBucket
	 * @param
	 * @Description
	 * @author hypnos
	 * @date 2022-04-11 03:57:49
	 */
	public static void createBucket(AmazonS3 client, String bucketName) {
		Bucket bucket = client.createBucket(bucketName);
		System.out.printf("创建的空间名为：%s", bucket.getName());
	}
	
	
	public static boolean isExistBucket(AmazonS3 client, String bucketName){
		boolean rs = true;
		try {
			HeadBucketRequest request = new HeadBucketRequest(bucketName);
			client.headBucket(request);
		} catch (AmazonS3Exception e) {
			rs = false;
		}
		return rs;
	}
	
	public static void putObject(AmazonS3 client) { 
		 String myBucketName = "mytest-bucket";
		 String objectInBucketKey = "objectInBucketKey1";
		 File testFile = new File("D:\\test.txt");
		 PutObjectRequest request = new PutObjectRequest(myBucketName,objectInBucketKey,testFile);
		 ObjectMetadata metadata = new ObjectMetadata();
		 CtyunBucketDataLocation dataLocation = new CtyunBucketDataLocation();
		 dataLocation.setType(CtyunBucketDataLocation.CtyunBucketDataType.Specified);
		 //List<String> dataRegions = new ArrayList<String>();
		 //dataRegions.add("QingDao");
		 //dataRegions.add("ShenZhen");
		 //dataLocation.setDataRegions(dataRegions);
		 metadata.setDataLocation(dataLocation);
		 request.setMetadata(metadata);
		 client.putObject(request);
	}
 
	
	
	
	public static AmazonS3 getAmazonS3(){
		ClientConfiguration clientConfig = new ClientConfiguration();
		// 设置连接的超时时间，单位毫秒
		clientConfig.setConnectionTimeout(30*1000); 
		// 设置 socket 超时时间，单位毫秒
		clientConfig.setSocketTimeout(30*1000) ; 
		// 设置 http
		clientConfig.setProtocol(Protocol.HTTP); 
		
		// 设置 V4 签名算法中负载是否参与签名，关于签名部分请参看《OOS 开发者文档》
		S3ClientOptions options = new S3ClientOptions();
		options.setPayloadSigningEnabled(true);
		
		// 创建 client
		AmazonS3 oosClient = new AmazonS3Client(new PropertiesCredentials(
				accessKey, secretAccessKey), clientConfig);
		// 设置 endpoint
		oosClient.setEndpoint(endpoint);
		// 设置选项
		oosClient.setS3ClientOptions(options);
		return oosClient;
	}
}
