2021-10-09

package GeneratePresignedURL;import java.io.IOException; import java.net.URL;import com.amazonaws.AmazonServiceException; import com.amazonaws.AmazonClientException; import com.amazonaws.ClientConfiguration; import com.amazonaws.HttpMethod; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSCredentials;import com.amazonaws.auth.AWSStaticCredentialsProvider;import com.amazonaws.auth.BasicAWSCredentials;import com.amazonaws.client.builder.AwsClientBuilder;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.GeneratePresignedUrlRequest;import java.util.Date;public class GeneratePresignedURL { public static AmazonS3 s3Client = null; public static void main(String[] args) throws IOException { String akey = “86H2PZFEG87N6R3D1CZ0”; String skey = “6VpDXiyHX9U0OMIvOk8mvN8nJOP8ZUKZGIyppyXS”; String endpoint = “http://10.255.20.148:7480”; String bucketName = “testbucket-php-test”; String objectKey = “license-1598694316085.enc”; String value = “S3SignerType”; try { AWSCredentials credentials = new BasicAWSCredentials(akey, skey); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP);// clientConfig.setSignerOverride(value);// AmazonS3 s3Client = new AmazonS3Client(credentials, clientConfig); // s3Client.setEndpoint(endpoint); EndpointConfiguration end_point = null; end_point = new AwsClientBuilder.EndpointConfiguration(endpoint, “us-east-1”); //创建连接,替换原AmazonS3Client接口 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withClientConfiguration(clientConfig) .withEndpointConfiguration(end_point) //.withPathStyleAccessEnabled(true) .build(); //设置预签名URL 在一小时后过期 java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60; expiration.setTime(expTimeMillis); //生成预签名URL System.out.println(new Date().toString()); System.out.println(“Generating pre-signed URL.”); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey) .withMethod(HttpMethod.GET) .withExpiration(expiration); URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); System.out.println("Pre-Signed URL: " + url.toString()); }catch(AmazonServiceException e) { e.printStackTrace(); }catch(AmazonClientException e) { // e.printStackTrace(); } } }

package GeneratePresignedURL;import java.io.IOException; import java.net.URL;import com.amazonaws.AmazonServiceException; import com.amazonaws.AmazonClientException; import com.amazonaws.ClientConfiguration; import com.amazonaws.HttpMethod; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSCredentials;import com.amazonaws.auth.AWSStaticCredentialsProvider;import com.amazonaws.auth.BasicAWSCredentials;import com.amazonaws.client.builder.AwsClientBuilder;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.GeneratePresignedUrlRequest;import java.util.Date;public class GeneratePresignedURL {		public static  AmazonS3 s3Client = null;	    public static void main(String[] args) throws IOException {		String akey = "86H2PZFEG87N6R3D1CZ0";        		String skey = "6VpDXiyHX9U0OMIvOk8mvN8nJOP8ZUKZGIyppyXS";        		String endpoint = "http://10.255.20.148:7480";                		String bucketName = "testbucket-php-test";        		String objectKey = "license-1598694316085.enc";				String value = "S3SignerType";        		try {                        			AWSCredentials credentials = new BasicAWSCredentials(akey, skey);            			ClientConfiguration clientConfig = new ClientConfiguration();            			clientConfig.setProtocol(Protocol.HTTP);//			clientConfig.setSignerOverride(value);//            AmazonS3 s3Client = new AmazonS3Client(credentials, clientConfig);            //			s3Client.setEndpoint(endpoint);				        EndpointConfiguration end_point = null;	        end_point = new AwsClientBuilder.EndpointConfiguration(endpoint, "us-east-1");	        //创建连接,替换原AmazonS3Client接口	        s3Client = AmazonS3ClientBuilder.standard()	        		.withCredentials(new AWSStaticCredentialsProvider(credentials))	        			.withClientConfiguration(clientConfig)	        				.withEndpointConfiguration(end_point)	        					//.withPathStyleAccessEnabled(true)	        						.build();			//设置预签名URL 在一小时后过期      			java.util.Date expiration = new java.util.Date();            			long expTimeMillis = expiration.getTime();            			expTimeMillis += 1000 * 60 * 60;            			expiration.setTime(expTimeMillis);			            //生成预签名URL   			System.out.println(new Date().toString());			System.out.println("Generating pre-signed URL.");            			GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey)				.withMethod(HttpMethod.GET)				.withExpiration(expiration);			URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);                			System.out.println("Pre-Signed URL: " + url.toString());        		}catch(AmazonServiceException e) {					e.printStackTrace();        		}catch(AmazonClientException e) {            			//           			e.printStackTrace();        		}    	} }

你可能感兴趣的:(笔记)