本文整理汇总了Java中com.amazonaws.services.s3.AmazonS3Client类的典型用法代码示例。如果您正苦于以下问题:Java AmazonS3Client类的具体用法?Java AmazonS3Client怎么用?Java AmazonS3Client使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AmazonS3Client类属于com.amazonaws.services.s3包,在下文中一共展示了AmazonS3Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadToS3
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
public static void uploadToS3() {
// upload to s3 bucket
AWSCredentials awsCredentials = SkillConfig.getAWSCredentials();
AmazonS3Client s3Client = awsCredentials != null ? new AmazonS3Client(awsCredentials) : new AmazonS3Client();
File folder = new File("c:/temp/morse/" + DOT + "/mp3/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
if (!s3Client.doesObjectExist("morseskill", DOT + "/" + file.getName())) {
PutObjectRequest s3Put = new PutObjectRequest("morseskill", DOT + "/" + file.getName(), file).withCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(s3Put);
System.out.println("Upload complete: " + file.getName());
}
else {
System.out.println("Skip as " + file.getName() + " already exists.");
}
}
}
}
示例2: shutdownOfSqsAndS3FactoryCreatedClientsOccursWhenS3DeleteObjectFails
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Test
public void shutdownOfSqsAndS3FactoryCreatedClientsOccursWhenS3DeleteObjectFails() {
AmazonSQSClient sqs = Mockito.mock(AmazonSQSClient.class);
AmazonS3Client s3 = Mockito.mock(AmazonS3Client.class);
String s3Id = "123";
SqsMessage m = new SqsMessage(RECEIPT_HANDLE, new byte[] {}, 1000, Optional.of(s3Id),
new SqsMessage.Service(Optional.of(() -> s3), () -> sqs, Optional.of(s3), sqs,
QUEUE, Optional.of(BUCKET)));
Mockito.when(sqs.deleteMessage(QUEUE, RECEIPT_HANDLE))
.thenReturn(new DeleteMessageResult());
Mockito.doThrow(new RuntimeException()).when(s3).deleteObject(BUCKET, s3Id);
try {
m.deleteMessage(Client.FROM_FACTORY);
Assert.fail();
} catch (RuntimeException e) {
// do nothing
}
InOrder inorder = Mockito.inOrder(sqs, s3);
inorder.verify(s3, Mockito.times(1)).deleteObject(BUCKET, s3Id);
inorder.verify(s3, Mockito.times(1)).shutdown();
inorder.verify(sqs, Mockito.times(1)).shutdown();
Mockito.verifyNoMoreInteractions(sqs, s3);
}
示例3: TestEndpoint
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
/**
* Test if custom endpoint is picked up.
* <p/>
* The test expects TEST_ENDPOINT to be defined in the Configuration
* describing the endpoint of the bucket to which TEST_FS_S3A_NAME points
* (f.i. "s3-eu-west-1.amazonaws.com" if the bucket is located in Ireland).
* Evidently, the bucket has to be hosted in the region denoted by the
* endpoint for the test to succeed.
* <p/>
* More info and the list of endpoint identifiers:
* http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
*
* @throws Exception
*/
@Test
public void TestEndpoint() throws Exception {
conf = new Configuration();
String endpoint = conf.getTrimmed(TEST_ENDPOINT, "");
if (endpoint.isEmpty()) {
LOG.warn("Custom endpoint test skipped as " + TEST_ENDPOINT + "config " +
"setting was not detected");
} else {
conf.set(Constants.ENDPOINT, endpoint);
fs = S3ATestUtils.createTestFileSystem(conf);
AmazonS3Client s3 = fs.getAmazonS3Client();
String endPointRegion = "";
// Differentiate handling of "s3-" and "s3." based endpoint identifiers
String[] endpointParts = StringUtils.split(endpoint, '.');
if (endpointParts.length == 3) {
endPointRegion = endpointParts[0].substring(3);
} else if (endpointParts.length == 4) {
endPointRegion = endpointParts[1];
} else {
fail("Unexpected endpoint");
}
assertEquals("Endpoint config setting and bucket location differ: ",
endPointRegion, s3.getBucketLocation(fs.getUri().getHost()));
}
}
示例4: init
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
public void init(Configuration conf, String keyPrefix) {
bucketName = conf.get(keyPrefix + S3_BUCKET_NAME);
String endpoint = conf.get(keyPrefix + S3_ENDPOINT_NAME);
String key = conf.get(keyPrefix + S3_ACCESS_KEY);
String secret = conf.get(keyPrefix + S3_ACCESS_SECRET);
System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY, key);
System.setProperty(SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY, secret);
AWSCredentialsProvider provider = new SystemPropertiesCredentialsProvider();
client = new AmazonS3Client(provider);
client.setEndpoint(endpoint);
override = conf.getBoolean(keyPrefix + "override", true);
acls = new AccessControlList();
acls.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
acls.grantPermission(GroupGrantee.AllUsers, Permission.Read);
acls.grantPermission(GroupGrantee.AllUsers, Permission.Write);
}
示例5: remove
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
public void remove(Object[] params) {
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
try {
s3client.deleteObject(new DeleteObjectRequest(bucketName, params[0].toString()));
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which " +
"means your request made it " +
"to Amazon S3, but was rejected with an error response" +
" for some reason.");
System.out.println("Error Message:" + ase.getMessage());
System.out.println("HTTP Status Code:" + ase.getStatusCode());
System.out.println("AWS Error Code:" + ase.getErrorCode());
System.out.println("Error Type:" + ase.getErrorType());
System.out.println("Request ID:" + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which " +
"means the client encountered " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
示例6: bindings
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
public Seq<Binding<?>> bindings(Environment environment, Configuration configuration) {
GlobalParams.AWS_ACCESS_KEY = Scala.orNull(configuration.getString("AWS_ACCESS_KEY", scala.Option.empty()));
GlobalParams.AWS_SECRET_KEY = Scala.orNull(configuration.getString("AWS_SECRET_KEY", scala.Option.empty()));
GlobalParams.AWS_S3_BUCKET = Scala.orNull(configuration.getString("AWS_S3_BUCKET", scala.Option.empty()));
String accessKey = GlobalParams.AWS_ACCESS_KEY;
String secretKey = GlobalParams.AWS_SECRET_KEY;
s3Bucket = GlobalParams.AWS_S3_BUCKET;
if ((accessKey != null) && (secretKey != null)) {
awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
amazonS3 = new AmazonS3Client(awsCredentials);
Logger.info("Using S3 Bucket: " + s3Bucket);
}
return seq(
bind(S3Plugin.class).to(S3PluginImpl.class)
);
}
示例7: webHookDump
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
public static String webHookDump(InputStream stream, String school, String extension) {
if (stream != null) {
extension = extension == null || extension.isEmpty() ? ".xml" : extension.contains(".") ? extension : "." + extension;
String fileName = "webhooks/" + school + "/" + school + "_" + Clock.getCurrentDateDashes() + "_" + Clock.getCurrentTime() + extension;
AmazonS3 s3 = new AmazonS3Client();
Region region = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(region);
try {
File file = CustomUtilities.inputStreamToFile(stream);
s3.putObject(new PutObjectRequest(name, fileName, file));
return CustomUtilities.fileToString(file);
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
示例8: load
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
public AmazonS3Client load(S3ClientKey clientKey) throws Exception {
logger.debug("Opening S3 client connection for {}", clientKey);
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setProtocol(clientKey.isSecure ? Protocol.HTTPS : Protocol.HTTP);
// Proxy settings (if configured)
clientConf.setProxyHost(clientKey.s3Config.get(Constants.PROXY_HOST));
if (clientKey.s3Config.get(Constants.PROXY_PORT) != null) {
clientConf.setProxyPort(Integer.valueOf(clientKey.s3Config.get(Constants.PROXY_PORT)));
}
clientConf.setProxyDomain(clientKey.s3Config.get(Constants.PROXY_DOMAIN));
clientConf.setProxyUsername(clientKey.s3Config.get(Constants.PROXY_USERNAME));
clientConf.setProxyPassword(clientKey.s3Config.get(Constants.PROXY_PASSWORD));
clientConf.setProxyWorkstation(clientKey.s3Config.get(Constants.PROXY_WORKSTATION));
if (clientKey.accessKey == null){
return new AmazonS3Client(new AnonymousAWSCredentialsProvider(), clientConf);
} else {
return new AmazonS3Client(new BasicAWSCredentials(clientKey.accessKey, clientKey.secretKey), clientConf);
}
}
示例9: amazonS3Client
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
/**
* S3 储存客户端
*
* @return 客户端
*/
@Bean
@ConditionalOnProperty(value = "bigbug.storage.s3.enable", havingValue = "true")
AmazonS3Client amazonS3Client() {
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
BasicAWSCredentials basicAWSCredentials =
new BasicAWSCredentials(
storageProperties.getStorage().getS3().getAccessKey(),
storageProperties.getStorage().getS3().getSecretKey());
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withClientConfiguration(clientConfig)
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
storageProperties.getStorage().getS3().getEndpoint(), Regions.DEFAULT_REGION.getName()))
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
}
示例10: S3ConfigurationCache
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
/**
* Creates a new S3 configuration cache.
* @param bucket The S3 bucket.
* @param prefix The S3 object prefix.
* @param pathPattern The path pattern.
* @param accessKey The (optional) S3 access key.
* @param secretKey The (optional) S3 secret key.
* @param region The AWS region (e.g. us-east-1).
* @throws IOException Thrown if the configuration cannot be read.
*/
public S3ConfigurationCache(String bucket, String prefix, String pathPattern,
String accessKey, String secretKey, String region) throws IOException {
this.bucket = bucket;
this.prefix = prefix;
this.pathPattern = pathPattern;
if (!StringUtils.isEmpty(accessKey)) {
s3 = AmazonS3Client.builder()
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(accessKey, secretKey)))
.withRegion(Regions.fromName(region))
.build();
} else {
s3 = AmazonS3Client.builder()
.withRegion(Regions.fromName(region))
.build();
}
}
示例11: S3StorageService
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
public S3StorageService() {
CloudStorageSettings settings = Settings.instance().getCloudStorage();
if (settings == null) {
throw new ConfigException("You are missing the section [cloudStorage]\naccessToken=...\nsecret=... in your stallion.toml");
}
if (empty(settings.getAccessToken())) {
throw new ConfigException("You are missing the setting accessKey in the stallion.toml section [cloudStorage]");
}
if (empty(settings.getSecret())) {
throw new ConfigException("You are missing the setting secret in the stallion.toml section [cloudStorage]");
}
accessToken = settings.getAccessToken();
secret = settings.getSecret();
AWSCredentials credentials = new BasicAWSCredentials(accessToken, secret);
client = new AmazonS3Client(credentials);
}
示例12: putObject
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
public PutObjectResult putObject(PutObjectRequest request) throws Exception
{
RefCountedClient holder = client.get();
AmazonS3Client amazonS3Client = holder.useClient();
try
{
return amazonS3Client.putObject(request);
}
finally
{
holder.release();
}
}
示例13: remove
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
public void remove(String accessKey) throws Exception {
LogUtils.debug(LOG_TAG, "Deleting file with access key: " + accessKey);
AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName);
List<KeyVersion> keys = new ArrayList<KeyVersion>();
keys.add(new KeyVersion(accessKey));
keys.add(new KeyVersion(accessKey + "_key"));
multiObjectDeleteRequest.setKeys(keys);
s3Client.deleteObjects(multiObjectDeleteRequest);
LogUtils.debug(LOG_TAG, "Deleted file with access key: " + accessKey);
}
示例14: list
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
@SuppressWarnings("unused")
public List<AwsFileMiniModel> list(String prefix) {
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
List<AwsFileMiniModel> files = new ArrayList();
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix);
ObjectListing objectListing;
do {
objectListing = s3client.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"
+ " (date = " + objectSummary.getLastModified() + ")");
files.add(new AwsFileMiniModel(objectSummary.getKey(), objectSummary.getLastModified()));
}
listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());
return files;
}
示例15: start
import com.amazonaws.services.s3.AmazonS3Client; //导入依赖的package包/类
@Override
public void start(Map<String, String> properties) {
configProperties = properties;
config = new RedshiftSinkTaskConfig(configProperties);
tempDir = new File(config.getTempOutputDir());
tempDir.mkdirs();
tempFiles = new HashMap<>();
writers = new HashMap<>();
List<String> fields = config.getFields();
if (fields.size() == 1 && fields.get(0).equals("*")) {
fields.clear();
}
serializer = new DefaultCopySerializer(fields);
s3 = new AmazonS3Client(config.getAwsCredentials());
}