本文整理匯總了Java中com.amazonaws.services.s3.model.S3Object類的典型用法代碼示例。如果您正苦於以下問題:Java S3Object類的具體用法?Java S3Object怎麽用?Java S3Object使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
S3Object類屬於com.amazonaws.services.s3.model包,在下文中一共展示了S3Object類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getMetaData
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
public ExternalResourceMetaData getMetaData(URI location, boolean revalidate) {
LOGGER.debug("Attempting to get resource metadata: {}", location);
S3Object s3Object = s3Client.getMetaData(location);
if (s3Object == null) {
return null;
}
try {
ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
return new DefaultExternalResourceMetaData(location,
objectMetadata.getLastModified().getTime(),
objectMetadata.getContentLength(),
objectMetadata.getContentType(),
objectMetadata.getETag(),
null); // Passing null for sha1 - TODO - consider using the etag which is an MD5 hash of the file (when less than 5Gb)
} finally {
IoActions.closeQuietly(s3Object);
}
}
示例2: getObject
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
@Override
public S3Object getObject(GetObjectRequest getObjectRequest)
throws AmazonClientException, AmazonServiceException {
// in ESBlobStoreContainerTestCase.java, the prefix is empty,
// so the key and blobName are equivalent to each other
String blobName = getObjectRequest.getKey();
if (!blobs.containsKey(blobName)) {
throw new AmazonS3Exception("[" + blobName + "] does not exist.");
}
// the HTTP request attribute is irrelevant for reading
S3ObjectInputStream stream = new S3ObjectInputStream(
blobs.get(blobName), null, false);
S3Object s3Object = new S3Object();
s3Object.setObjectContent(stream);
return s3Object;
}
示例3: doGetS3Object
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
private S3Object doGetS3Object(URI uri, boolean isLightWeight) {
S3RegionalResource s3RegionalResource = new S3RegionalResource(uri);
String bucketName = s3RegionalResource.getBucketName();
String s3BucketKey = s3RegionalResource.getKey();
configureClient(s3RegionalResource);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, s3BucketKey);
if (isLightWeight) {
//Skip content download
getObjectRequest.setRange(0, 0);
}
try {
return amazonS3Client.getObject(getObjectRequest);
} catch (AmazonServiceException e) {
String errorCode = e.getErrorCode();
if (null != errorCode && errorCode.equalsIgnoreCase("NoSuchKey")) {
return null;
}
throw ResourceExceptions.getFailed(uri, e);
}
}
示例4: getFileByLocationId
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
/**
* Performs a {@link GetObjectRequest} to the S3 bucket by file id for the file
*
* @param fileLocationId Id of the file to search for
* @return file found from S3
*/
@Override
public InputStream getFileByLocationId(String fileLocationId) {
final String bucketName = environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE);
if (Strings.isNullOrEmpty(bucketName)) {
API_LOG.warn("No bucket name is specified.");
return null;
}
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileLocationId);
S3Object s3Object = amazonS3.getObject(getObjectRequest);
API_LOG.info("Successfully retrieved the file from S3 bucket {}", getObjectRequest.getBucketName());
return s3Object.getObjectContent();
}
示例5: readBlob
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
@Override
public InputStream readBlob(String blobName) throws IOException {
int retry = 0;
while (retry <= blobStore.numberOfRetries()) {
try {
S3Object s3Object = SocketAccess.doPrivileged(() -> blobStore.client().getObject(blobStore.bucket(), buildKey(blobName)));
return s3Object.getObjectContent();
} catch (AmazonClientException e) {
if (blobStore.shouldRetry(e) && (retry < blobStore.numberOfRetries())) {
retry++;
} else {
if (e instanceof AmazonS3Exception) {
if (404 == ((AmazonS3Exception) e).getStatusCode()) {
throw new NoSuchFileException("Blob object [" + blobName + "] not found: " + e.getMessage());
}
}
throw e;
}
}
}
throw new BlobStoreException("retries exhausted while attempting to access blob object [name:" + blobName + ", bucket:" + blobStore.bucket() + "]");
}
示例6: shouldUploadInParallel
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
/**
* Tests if an object can be uploaded asynchronously
*
* @throws Exception not expected
*/
@Test
public void shouldUploadInParallel() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final TransferManager transferManager = createDefaultTransferManager();
final Upload upload =
transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
final UploadResult uploadResult = upload.waitForUploadResult();
assertThat(uploadResult.getKey(), equalTo(UPLOAD_FILE_NAME));
final S3Object getResult = s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat(getResult.getKey(), equalTo(UPLOAD_FILE_NAME));
}
示例7: writeLocalFile
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
private void writeLocalFile(
final S3Object s3Object,
final File file) {
try(FileOutputStream fileOutputStream
= new FileOutputStream(file)) {
IOUtils.copy(
s3Object.getObjectContent(),
fileOutputStream);
} catch (IOException ioException) {
throw new SecretsLockerException(
ioException);
}
file.deleteOnExit();
}
示例8: getInventoryReportToString
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
/**
* Get the original inventory report from S3, unzip it, and transfer it into a String format.
* @return inventReport String
* @throws IOException when getting object from S3 fails
* or the checksum of the inventory report and the checksum specified in the manifest file not match
*/
public String getInventoryReportToString() throws IOException {
String inventReportKey = locator.getKey();
String bucketName = inventoryManifest.getSourceBucket();
try (S3Object s3InventoryReport = s3Client.getObject(
new GetObjectRequest(bucketName, inventReportKey))) {
InputStream objectData = s3InventoryReport.getObjectContent();
byte[] zippedData = IOUtils.toByteArray(objectData);
String actualChecksum = DigestUtils.md5Hex(zippedData);
String expectedChecksum = locator.getMD5checksum();
if (!actualChecksum.equals(expectedChecksum)) {
throw new ChecksumMismatchException (expectedChecksum, actualChecksum);
}
return IOUtils.toString(new GZIPInputStream(new ByteArrayInputStream(zippedData)));
}
}
示例9: getInventoryManifest
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
/**
* Check if the MD5s of manifest.json and manifest.checksum equal
* if so, pull out the manifest file and map it into a POJO
* @return inventoryManifestStorage InventoryManifest, which stores all the elements of the manifest.json file
*/
public InventoryManifest getInventoryManifest() throws Exception {
// Get manifest.json and transfer it to String
GetObjectRequest requestJson = new GetObjectRequest(bucketName, bucketKeyJson);
S3Object jsonObject = s3Client.getObject(requestJson);
String jsonFile = inputStreamToString(jsonObject.getObjectContent());
jsonObject.close();
// Get manifest.checksum and transfer it to String with no whitespace
GetObjectRequest requestChecksum = new GetObjectRequest(bucketName, bucketKeyChecksum);
S3Object checksumObject = s3Client.getObject(requestChecksum);
String expectedChecksum = inputStreamToString(checksumObject.getObjectContent())
.replaceAll("\\s","");
checksumObject.close();
// Compare manifest.json and manifest.checksum's MD5 value
String actualChecksum = DigestUtils.md5Hex(jsonFile);
if (!actualChecksum.equals(expectedChecksum)) {
throw new ChecksumMismatchException (expectedChecksum, actualChecksum);
}
return mapper.readValue(jsonFile, InventoryManifest.class);
}
示例10: putObject
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
@SuppressWarnings("resource")
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException {
putObjectRequests.add(putObjectRequest);
S3Object s3Object = new S3Object();
s3Object.setBucketName(putObjectRequest.getBucketName());
s3Object.setKey(putObjectRequest.getKey());
if (putObjectRequest.getFile() != null) {
try {
s3Object.setObjectContent(new FileInputStream(putObjectRequest.getFile()));
} catch (FileNotFoundException e) {
throw new AmazonServiceException("Cannot store the file object.", e);
}
} else {
s3Object.setObjectContent(putObjectRequest.getInputStream());
}
objects.add(s3Object);
PutObjectResult putObjectResult = new PutObjectResult();
putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264");
return putObjectResult;
}
示例11: shouldCopyObject
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
/**
* Puts an Object; Copies that object to a new bucket; Downloads the object from the new
* bucket; compares checksums
* of original and copied object
*
* @throws Exception if an Exception occurs
*/
@Test
public void shouldCopyObject() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationBucket";
final String destinationKey = "copyOf/" + sourceKey;
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
s3Client.copyObject(copyObjectRequest);
final com.amazonaws.services.s3.model.S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Sourcefile and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
}
示例12: load
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
@Override
public boolean load(BuildCacheKey key, BuildCacheEntryReader reader) {
if (s3.doesObjectExist(bucketName, key.getHashCode())) {
logger.info("Found cache item '{}' in S3 bucket", key.getHashCode());
S3Object object = s3.getObject(bucketName, key.getHashCode());
try (InputStream is = object.getObjectContent()) {
reader.readFrom(is);
return true;
} catch (IOException e) {
throw new BuildCacheException("Error while reading cache object from S3 bucket", e);
}
} else {
logger.info("Did not find cache item '{}' in S3 bucket", key.getHashCode());
return false;
}
}
示例13: copyOneObject
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
@Test
public void copyOneObject() throws Exception {
client.putObject("source", "data", inputData);
Path sourceBaseLocation = new Path("s3://source/");
Path replicaLocation = new Path("s3://target/");
List<Path> sourceSubLocations = new ArrayList<>();
S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
Metrics metrics = s3s3Copier.copy();
assertThat(metrics.getBytesReplicated(), is(7L));
assertThat(metrics.getMetrics().get(S3S3CopierMetrics.Metrics.TOTAL_BYTES_TO_REPLICATE.name()), is(7L));
S3Object object = client.getObject("target", "data");
String data = IOUtils.toString(object.getObjectContent());
assertThat(data, is("bar foo"));
assertThat(registry.getGauges().containsKey(RunningMetrics.S3S3_CP_BYTES_REPLICATED.name()), is(true));
}
示例14: copyMultipleObjects
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
@Test
public void copyMultipleObjects() throws Exception {
// Making sure we only request 1 file at the time so we need to loop
ListObjectsRequestFactory mockListObjectRequestFactory = Mockito.mock(ListObjectsRequestFactory.class);
when(mockListObjectRequestFactory.newInstance()).thenReturn(new ListObjectsRequest().withMaxKeys(1));
client.putObject("source", "bar/data1", inputData);
client.putObject("source", "bar/data2", inputData);
Path sourceBaseLocation = new Path("s3://source/bar/");
Path replicaLocation = new Path("s3://target/foo/");
List<Path> sourceSubLocations = new ArrayList<>();
S3S3Copier s3s3Copier = new S3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation, s3ClientFactory,
transferManagerFactory, mockListObjectRequestFactory, registry, s3S3CopierOptions);
Metrics metrics = s3s3Copier.copy();
assertThat(metrics.getBytesReplicated(), is(14L));
S3Object object1 = client.getObject("target", "foo/data1");
String data1 = IOUtils.toString(object1.getObjectContent());
assertThat(data1, is("bar foo"));
S3Object object2 = client.getObject("target", "foo/data2");
String data2 = IOUtils.toString(object2.getObjectContent());
assertThat(data2, is("bar foo"));
}
示例15: getS3Value
import com.amazonaws.services.s3.model.S3Object; //導入依賴的package包/類
/**
* Attempt to fetch a secret from S3.
*
* @param s3path where to fetch it from
* @return the content of the file found on S3
* @throws IOException on problems streaming the content of the file
* @throws AmazonS3Exception on problems communicating with amazon
*/
private String getS3Value(final SecretPath s3path) throws IOException, AmazonS3Exception {
LOG.info("Fetching secret from s3://" + s3path.bucket + "/" + s3path.key);
if (s3Client == null) {
if (awsCredentialsProvider != null) {
s3Client = AmazonS3ClientBuilder.standard().withCredentials(awsCredentialsProvider)
.build();
} else {
s3Client = AmazonS3ClientBuilder.standard().build();
}
}
final S3Object s3object
= s3Client.getObject(new GetObjectRequest(s3path.bucket, s3path.key));
final BufferedReader reader
= new BufferedReader(new InputStreamReader(s3object.getObjectContent()));
final StringBuilder b = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
b.append(line);
}
LOG.info("Found secret");
reader.close();
return b.toString();
}