本文整理匯總了Java中com.amazonaws.services.s3.model.S3ObjectInputStream類的典型用法代碼示例。如果您正苦於以下問題:Java S3ObjectInputStream類的具體用法?Java S3ObjectInputStream怎麽用?Java S3ObjectInputStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
S3ObjectInputStream類屬於com.amazonaws.services.s3.model包,在下文中一共展示了S3ObjectInputStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testGet
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void testGet() {
AmazonS3 client = mock(AmazonS3.class);
S3StoreService service = new S3StoreService(client, S3_BUCKET, S3_PREFIX);
String path = "path";
String value = "value";
ArgumentCaptor<GetObjectRequest> request = ArgumentCaptor.forClass(GetObjectRequest.class);
S3Object s3Object = new S3Object();
s3Object.setObjectContent(new S3ObjectInputStream(IOUtils.toInputStream(value), mock(HttpRequestBase.class)));
when(client.getObject(request.capture())).thenReturn(s3Object);
// invoke method under test
Optional<String> result = service.get(path);
assertTrue(result.isPresent());
assertEquals(value, result.get());
assertEquals(S3_BUCKET, request.getValue().getBucketName());
assertEquals(S3_PREFIX + "/" + path, request.getValue().getKey());
}
示例2: getObject
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的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: getInventReportSuccess
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void getInventReportSuccess() throws Exception {
testLocator.setMD5checksum(testMD5);
testManifest.setFileSchema("storageClass, size");
reportRetriever = new InventoryReportRetriever(mockS3Client, testLocator, testManifest);
String expectedInventoryReportString = "testString";
byte[] expectedInventoryReportBytes = inventReportBytes(expectedInventoryReportString);
when(mockS3Object.getObjectContent()).thenReturn(new S3ObjectInputStream(
new ByteArrayInputStream(expectedInventoryReportBytes), null));
when(mockS3Client.getObject(getObjectRequestCaptor.capture())).thenReturn(mockS3Object);
String result = reportRetriever.getInventoryReportToString();
assertThat(result, is(expectedInventoryReportString));
GetObjectRequest request = getObjectRequestCaptor.getValue();
assertThat(request.getBucketName(), is("testBucket"));
assertThat(request.getKey(), is("testInventReportKey"));
}
示例4: getInventoryManifestSuccess
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void getInventoryManifestSuccess() throws Exception {
InventoryManifest expectedManifest = manifest();
byte[] expectedManifestBytes = manifestBytes(expectedManifest);
when(mockS3JsonObject.getObjectContent()).thenReturn(new S3ObjectInputStream(
new ByteArrayInputStream(expectedManifestBytes), null));
String expectedChecksum = "a6121a6a788be627a68d7e9def9f6968";
byte[] expectedChecksumBytes = expectedChecksum.getBytes(StandardCharsets.UTF_8);
when(mockS3ChecksumObject.getObjectContent()).thenReturn(new S3ObjectInputStream(
new ByteArrayInputStream(expectedChecksumBytes), null));
when(mockS3Client.getObject(getObjectRequestCaptor.capture()))
.thenReturn(mockS3JsonObject)
.thenReturn(mockS3ChecksumObject);
InventoryManifest result = retriever.getInventoryManifest();
assertThat(result, is(expectedManifest));
List<GetObjectRequest> request = getObjectRequestCaptor.getAllValues();
assertThat(request.get(0).getBucketName(), is("testBucketName"));
assertThat(request.get(0).getKey(), is("testBucketKey/manifest.json"));
assertThat(request.get(1).getBucketName(), is("testBucketName"));
assertThat(request.get(1).getKey(), is("testBucketKey/manifest.checksum"));
}
示例5: getInventoryManifestMD5Mismatch
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test (expected = ChecksumMismatchException.class)
public void getInventoryManifestMD5Mismatch() throws Exception {
InventoryManifest expectedManifest = manifest();
byte[] expectedManifestBytes = manifestBytes(expectedManifest);
byte[] errorBytes = "ERROR".getBytes();
byte[] wrongManifestBytes = ArrayUtils.addAll(expectedManifestBytes, errorBytes);
when(mockS3JsonObject.getObjectContent()).thenReturn(new S3ObjectInputStream(
new ByteArrayInputStream(wrongManifestBytes), null));
String expectedChecksum = "37289f10a76751046658f6c5e0ab41d9";
byte[] expectedChecksumBytes = expectedChecksum.getBytes(StandardCharsets.UTF_8);
when(mockS3ChecksumObject.getObjectContent()).thenReturn(new S3ObjectInputStream(
new ByteArrayInputStream(expectedChecksumBytes), null));
when(mockS3Client.getObject(getObjectRequestCaptor.capture())).
thenReturn(mockS3JsonObject)
.thenReturn(mockS3ChecksumObject);
retriever.getInventoryManifest();
}
示例6: contentTest
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void contentTest() throws Exception {
URL url = this.getClass().getResource("../../../../amazon-aws-logo.jpg");
String tmpFileName = url.getFile();
File file = new File(tmpFileName);
String fileName = file.getName();
InputStream is = url.openStream();
String contentType = URLConnection.guessContentTypeFromStream(is);
contentHelper.uploadContent(contentType, file.length(), bucketName, fileName, is);
Thread.sleep(500);
boolean doesObjectExist = s3Client.doesObjectExist(bucketName, fileName);
Assert.assertTrue(doesObjectExist);
S3ObjectInputStream inputStream = contentHelper.downloadContent(bucketName, fileName);
Assert.assertNotNull(inputStream);
contentHelper.deleteContent(bucketName, fileName);
Thread.sleep(500);
doesObjectExist = s3Client.doesObjectExist(bucketName, fileName);
Assert.assertFalse(doesObjectExist);
}
示例7: readS3Object
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
/**
* Reads bytes into {@link #target}, until either the end of {@link #target} or the end of the S3 object is
* reached.
*
* @param s3Object the S3 object
* @param getObjectRequest the S3 get-object request used for retrieving {@code s3Object}
* @return the total size of the S3 object
* @throws AmazonClientException if a call to {@link S3ObjectInputStream#read(byte[], int, int)} does not read
* any bytes even though it should have
* @throws IOException if a call to {@link S3ObjectInputStream#read(byte[], int, int)} throws an I/O exception
*/
private long readS3Object(@Nullable S3Object s3Object, GetObjectRequest getObjectRequest) throws IOException {
long totalSize;
if (s3Object == null) {
totalSize = s3Client.getObjectMetadata(bucketName, key).getInstanceLength();
if (offsetInS3Object < totalSize) {
throw new AmazonClientException(String.format(
"Could not read %s (range: %s), because AmazonS3#getClient() returned null.",
key, Arrays.toString(getObjectRequest.getRange())
));
}
} else {
totalSize = s3Object.getObjectMetadata().getInstanceLength();
// Note that the (int) cast is safe because target.length is of type int.
int remainingBytesToRead
= (int) Math.max(0, Math.min(target.length - posInTarget, totalSize - offsetInS3Object));
S3ObjectInputStream inputStream = s3Object.getObjectContent();
int bytesRead;
while (remainingBytesToRead > 0) {
// read() promises to read "up to" remainingBytesToRead bytes. There is no guarantee that
// this many bytes are read, even if enough bytes are available. In fact, experiments showed
// that read() sometimes only returns 2^15 bytes.
bytesRead = inputStream.read(target, posInTarget, remainingBytesToRead);
posInTarget += bytesRead;
remainingBytesToRead -= bytesRead;
if (bytesRead <= 0) {
// This should not happen and indicates a logical bug. We therefore fail here.
throw new AmazonClientException(String.format(
"Could not read %s (range: %s). Requested %d bytes from input stream, but "
+ "S3ObjectInputStream#read() returned %d.",
key, Arrays.toString(getObjectRequest.getRange()),
remainingBytesToRead, bytesRead
));
}
}
}
return totalSize;
}
示例8: testGetNoSuchKey
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void testGetNoSuchKey() {
AmazonS3 client = mock(AmazonS3.class);
S3StoreService service = new S3StoreService(client, S3_BUCKET, S3_PREFIX);
String path = "path";
String value = "value";
ArgumentCaptor<GetObjectRequest> request = ArgumentCaptor.forClass(GetObjectRequest.class);
S3Object s3Object = new S3Object();
s3Object.setObjectContent(new S3ObjectInputStream(IOUtils.toInputStream(value), mock(HttpRequestBase.class)));
AmazonServiceException error = new AmazonServiceException("fake expected exception");
error.setErrorCode("NoSuchKey");
when(client.getObject(request.capture())).thenThrow(error);
// invoke method under test
Optional<String> result = service.get(path);
assertFalse(result.isPresent());
assertEquals(S3_BUCKET, request.getValue().getBucketName());
assertEquals(S3_PREFIX + "/" + path, request.getValue().getKey());
}
示例9: testGetIOException
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void testGetIOException() throws IOException {
AmazonS3 client = mock(AmazonS3.class);
S3StoreService service = new S3StoreService(client, S3_BUCKET, S3_PREFIX);
InputStream is = mock(InputStream.class);
when(is.read()).thenThrow(new IOException("fake exception"));
S3Object s3Object = new S3Object();
s3Object.setObjectContent(new S3ObjectInputStream(is, mock(HttpRequestBase.class)));
when(client.getObject(any())).thenReturn(s3Object);
try {
// invoke method under test
service.get("some-path");
fail("expected exception not thrown");
} catch (UnexpectedDataEncodingException ex) {
assertTrue(ex.getMessage().contains("Unable to read contents of S3 object"));
}
}
示例10: testAwsPrivateKeyStore
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Test
public void testAwsPrivateKeyStore() throws Exception {
String bucketName = "my_bucket";
String keyName = "my_key";
String expected = "my_value";
AmazonS3 s3 = Mockito.mock(AmazonS3.class);
AWSKMS kms = Mockito.mock(AWSKMS.class);
S3Object s3Object = Mockito.mock(S3Object.class);
Mockito.when(s3.getObject(bucketName, keyName)).thenReturn(s3Object);
InputStream is = new ByteArrayInputStream( expected.getBytes() );
S3ObjectInputStream s3ObjectInputStream = new S3ObjectInputStream(is, null);
Mockito.when(s3Object.getObjectContent()).thenReturn(s3ObjectInputStream);
String result = expected;
ByteBuffer buffer = ByteBuffer.wrap(result.getBytes());
DecryptResult decryptResult = Mockito.mock(DecryptResult.class);
Mockito.when(kms.decrypt(Mockito.any(DecryptRequest.class))).thenReturn(decryptResult);
Mockito.when(decryptResult.getPlaintext()).thenReturn(buffer);
AwsPrivateKeyStore awsPrivateKeyStore = new AwsPrivateKeyStore(s3, kms);
String actual = awsPrivateKeyStore.getApplicationSecret(bucketName, keyName);
Assert.assertEquals(actual, expected);
}
示例11: getSignedDomain
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
SignedDomain getSignedDomain(AmazonS3 s3, String domainName) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("AWSS3ChangeLog: getting signed domain {}", domainName);
}
SignedDomain signedDomain = null;
try {
S3Object object = s3.getObject(s3BucketName, domainName);
try (S3ObjectInputStream s3is = object.getObjectContent()) {
byte[] data = ByteStreams.toByteArray(s3is);
signedDomain = JSON.fromBytes(data, SignedDomain.class);
}
} catch (Exception ex) {
LOGGER.error("AWSS3ChangeLog: getSignedDomain - unable to get domain {} error: {}",
domainName, ex.getMessage());
}
return signedDomain;
}
示例12: readData
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
/**
* S3 block read would be achieved through the AmazonS3 client. Following
* are the steps to achieve: (1) Create the objectRequest from bucketName
* and filePath. (2) Set the range to the above created objectRequest. (3)
* Get the object portion through AmazonS3 client API. (4) Get the object
* content from the above object portion.
*
* @param bytesFromCurrentOffset
* bytes read till now from current offset
* @param bytesToFetch
* the number of bytes to be fetched
* @return the number of bytes read, -1 if 0 bytes read
* @throws IOException
*/
@Override
protected int readData(final long bytesFromCurrentOffset, final int bytesToFetch) throws IOException
{
GetObjectRequest rangeObjectRequest = new GetObjectRequest(s3Params.bucketName, s3Params.filePath);
rangeObjectRequest.setRange(offset + bytesFromCurrentOffset, offset + bytesFromCurrentOffset + bytesToFetch - 1);
S3Object objectPortion = s3Params.s3Client.getObject(rangeObjectRequest);
S3ObjectInputStream wrappedStream = objectPortion.getObjectContent();
buffer = ByteStreams.toByteArray(wrappedStream);
wrappedStream.close();
int bufferLength = buffer.length;
if (bufferLength <= 0) {
return -1;
}
return bufferLength;
}
示例13: readEntity
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
/**
* S3 block read would be achieved through the AmazonS3 client. Following are the steps to achieve:
* (1) Create the objectRequest from bucketName and filePath.
* (2) Set the range to the above created objectRequest.
* (3) Get the object portion through AmazonS3 client API.
* (4) Get the object content from the above object portion.
* @return the block entity
* @throws IOException
*/
@Override
protected Entity readEntity() throws IOException
{
entity.clear();
GetObjectRequest rangeObjectRequest = new GetObjectRequest(
bucketName, filePath);
rangeObjectRequest.setRange(offset, blockMetadata.getLength() - 1);
S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
S3ObjectInputStream wrappedStream = objectPortion.getObjectContent();
byte[] record = ByteStreams.toByteArray(wrappedStream);
entity.setUsedBytes(record.length);
entity.setRecord(record);
wrappedStream.close();
return entity;
}
示例14: readFileData
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
@Override
public InputStream readFileData(FileData fileData) {
String path = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(extraPath + fileData.getPath() + "/" + fileData.getFileName()));
path = StringUtils.stripStart(path, "/");
InputStream ret = null;
S3ObjectInputStream objectContent = null;
try {
S3Object object = s3Client.getObject(bucketName, path);
if (object != null) {
ByteArrayOutputStream temp = new ByteArrayOutputStream();
objectContent = object.getObjectContent();
IOUtils.copy(objectContent, temp);
ret = new ByteArrayInputStream(temp.toByteArray());
if (compress) {
ret = new GZIPInputStream(ret);
}
}
} catch (Exception e) {
LOG.error("Error getting File: " + e, e);
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(objectContent);
}
return ret;
}
示例15: getFile
import com.amazonaws.services.s3.model.S3ObjectInputStream; //導入依賴的package包/類
/**
*
* @param key
* @return
*/
public InputStream getFile(String bucketName, String path) {
InputStream ret = null;
S3ObjectInputStream objectContent = null;
try {
S3Object object = s3Client.getObject(bucketName, path);
if (object != null) {
ByteArrayOutputStream temp = new ByteArrayOutputStream();
objectContent = object.getObjectContent();
IOUtils.copy(objectContent, temp);
ret = new ByteArrayInputStream(temp.toByteArray());
}
} catch (Exception e) {
LOG.error("Error getting File: " + e, e);
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(objectContent);
}
return ret;
}