我正在开发一款产品(支持V4 Auth),其功能类似于Amazon S3,同样的Amazon S3 API也可用于在我的产品上执行S3操作。
我使用Java代码遇到的问题是我能够在存储桶中创建Bucket / PUT对象,但不能通过V4身份验证,请求通过V2 Auth进行,我不想要。我正在使用最新的AWS-JAVA-SDK进行S3操作,但请求仍然是使用V2 auth。我不知道在我的代码中需要做什么,以便请求使用V4 Auth(而不是V2 Auth)。
我的代码 -
package com.amplidata.awstest;
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;
import org.junit.Test;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
/**
* This sample demonstrates how to make basic requests to Amazon S3 using the
* AWS SDK for Java.
* <p>
* <b>Prerequisites:</b> You must have a valid Amazon Web Services developer
* account, and be signed up to use Amazon S3. For more information on Amazon
* S3, see http://aws.amazon.com/s3.
* <p>
* <b>Important:</b> Be sure to fill in your AWS access credentials in the
* AwsCredentials.properties file before you try to run this sample.
* http://aws.amazon.com/security-credentials
*/
public class S3Sample {
private AmazonS3Client s3client;
@Test
public void s3operation() throws IOException {
/*
* This credentials provider implementation loads your AWS credentials
* from a properties file at the root of your classpath.
*
* Important: Be sure to fill in your AWS access credentials in the
* AwsCredentials.properties file before you try to run this sample.
* http://aws.amazon.com/security-credentials
*/
// Cut Example Code BEGIN
// AmazonS3 s3 = new AmazonS3Client(new
// ClasspathPropertiesFileCredentialsProvider());
// Region usWest2 = Region.getRegion(Regions.US_WEST_2);
// s3.setRegion(usWest2);
// Cut Example Code END
// Eucalyptus Code Changes BEGIN
// create a credential for client
final String username = "iwglsxxxxxv7a9b8e8838ec";
final String password = "3eni46tq4kokcyxxxxxxxnf4stk7fqgxxxxxxxmkdus";
//final String regionName = "us-east-1";
BasicAWSCredentials credentials = new BasicAWSCredentials(username,
password);
AmazonS3 s3 = new AmazonS3Client(credentials);
final String serviceurl = "http://192.168.44.2:8080/";
s3.setEndpoint(serviceurl);
S3ClientOptions s3ClientOptions = new S3ClientOptions();
s3ClientOptions.setPathStyleAccess(true);
s3.setS3ClientOptions(s3ClientOptions);
// Eucalyptus Code Changes END
String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
String key = "MyObjectKey";
System.out.println("===========================================");
System.out.println("Getting Started with Amazon S3");
System.out.println("===========================================\n");
/*
* Create a new S3 bucket - Amazon S3 bucket names are globally
* unique, so once a bucket name has been taken by any user, you
* can't create another bucket with that same name.
*
* You can optionally specify a location for your bucket if you want
* to keep your data closer to your applications or users.
*/
System.out.println("Creating bucket " + bucketName + "\n");
s3.createBucket(bucketName);
/*
* List the buckets in your account
*/
System.out.println("Listing buckets");
for (Bucket bucket : s3.listBuckets()) {
System.out.println(" - " + bucket.getName());
}
System.out.println();
/*
* Upload an object to your bucket - You can easily upload a file to
* S3, or upload directly an InputStream if you know the length of
* the data in the stream. You can also specify your own metadata
* when uploading to S3, which allows you set a variety of options
* like content-type and content-encoding, plus additional metadata
* specific to your applications.
*/
//System.setProperty(SDKGlobalConfiguration.ENFORCE_S3_SIGV4_SYSTEM_PROPERTY, "true");
//s3client.setRegion(Region.getRegion(Regions.US_EAST_1));
System.out.println("Uploading a new object to S3 from a file\n");
s3.putObject(new PutObjectRequest(bucketName,key,
createSampleFile()));
}
private static File createSampleFile() throws IOException {
File file = File.createTempFile("aws-java-sdk-", ".txt");
file.deleteOnExit();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.write("01234567890112345678901234\n");
writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
writer.write("01234567890112345678901234\n");
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.close();
return file;
}
}