本文整理汇总了Java中com.amazonaws.util.IOUtils.toString方法的典型用法代码示例。如果您正苦于以下问题:Java IOUtils.toString方法的具体用法?Java IOUtils.toString怎么用?Java IOUtils.toString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.util.IOUtils
的用法示例。
在下文中一共展示了IOUtils.toString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
* Helper method that parses a JSON object from a resource on the classpath
* as an instance of the provided type.
*
* @param resource
* the path to the resource (relative to this class)
* @param clazz
* the type to parse the JSON into
*/
public static <T> T parse(String resource, Class<T> clazz)
throws IOException {
InputStream stream = TestUtils.class.getResourceAsStream(resource);
try {
if (clazz == S3Event.class) {
String json = IOUtils.toString(stream);
S3EventNotification event = S3EventNotification.parseJson(json);
@SuppressWarnings("unchecked")
T result = (T) new S3Event(event.getRecords());
return result;
} else if (clazz == SNSEvent.class) {
return snsEventMapper.readValue(stream, clazz);
} else if (clazz == DynamodbEvent.class) {
return dynamodbEventMapper.readValue(stream, clazz);
} else {
return mapper.readValue(stream, clazz);
}
} finally {
stream.close();
}
}
示例2: parse
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
* Helper method that parses a JSON object from a resource on the classpath
* as an instance of the provided type.
*
* @param resource the path to the resource (relative to this class)
* @param clazz the type to parse the JSON into
*/
public static <T> T parse(String resource, Class<T> clazz)
throws IOException {
InputStream stream = TestUtils.class.getResourceAsStream(resource);
try {
if (clazz == S3Event.class) {
String json = IOUtils.toString(stream);
S3EventNotification event = S3EventNotification.parseJson(json);
@SuppressWarnings("unchecked")
T result = (T) new S3Event(event.getRecords());
return result;
} else {
return mapper.readValue(stream, clazz);
}
} finally {
stream.close();
}
}
示例3: copyOneObject
import com.amazonaws.util.IOUtils; //导入方法依赖的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));
}
示例4: copyMultipleObjects
import com.amazonaws.util.IOUtils; //导入方法依赖的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"));
}
示例5: handleErrorResponse
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
private void handleErrorResponse(InputStream errorStream, int statusCode, String responseMessage) throws IOException {
String errorCode = null;
// Parse the error stream returned from the service.
if(errorStream != null) {
String errorResponse = IOUtils.toString(errorStream);
try {
JsonNode node = Jackson.jsonNodeOf(errorResponse);
JsonNode code = node.get("code");
JsonNode message = node.get("message");
if (code != null && message != null) {
errorCode = code.asText();
responseMessage = message.asText();
}
} catch (Exception exception) {
LOG.debug("Unable to parse error stream");
}
}
AmazonServiceException ase = new AmazonServiceException(responseMessage);
ase.setStatusCode(statusCode);
ase.setErrorCode(errorCode);
throw ase;
}
示例6: getBaseProfile
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Override
protected Profile getBaseProfile(String name, String version, String outputFile) {
try {
return new Profile(name,
version,
outputFile,
IOUtils.toString(profileRegistry.readProfile(getArtifact().getName(), version, name))
);
} catch (RetrofitError | IOException e) {
throw new HalException(
new ConfigProblemBuilder(FATAL,
"Unable to retrieve profile \"" + name + "\": " + e.getMessage())
.build(),
e
);
}
}
示例7: decryptFile
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
* {@inheritDoc }
*/
@Override
public String decryptFile(
final String encryptedFilename) {
final KmsMasterKeyProvider provider
= new KmsMasterKeyProvider(
new DefaultAWSCredentialsProviderChain());
final AwsCrypto awsCrypto
= new AwsCrypto();
try (final FileInputStream fileInputStream
= new FileInputStream(
encryptedFilename);
final CryptoInputStream<?> decryptingStream
= awsCrypto
.createDecryptingStream(
provider,
fileInputStream)) {
return IOUtils.toString(
decryptingStream);
} catch (IOException exception) {
throw new DecryptionException(exception);
}
}
示例8: readFileFromS3
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
* Reads a file from S3 into a String object
* @param s3Uri (eg. s3://bucket/file.ext)
* @return String containing the content of the file in S3
* @throws IOException if error reading file
*/
public String readFileFromS3(String s3Uri) throws IOException {
AmazonS3URI s3FileUri = new AmazonS3URI(s3Uri);
S3Object s3object = amazonS3Client.getObject(new GetObjectRequest(s3FileUri.getBucket(), s3FileUri.getKey()));
return IOUtils.toString(s3object.getObjectContent());
}
示例9: copyOneObjectUsingKeys
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Test
public void copyOneObjectUsingKeys() throws Exception {
client.putObject("source", "bar/data", inputData);
Path sourceBaseLocation = new Path("s3://source/bar/");
Path replicaLocation = new Path("s3://target/foo/");
List<Path> sourceSubLocations = new ArrayList<>();
S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
s3s3Copier.copy();
S3Object object = client.getObject("target", "foo/data");
String data = IOUtils.toString(object.getObjectContent());
assertThat(data, is("bar foo"));
}
示例10: copyOneObjectPartitioned
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Test
public void copyOneObjectPartitioned() throws Exception {
client.putObject("source", "year=2016/data", inputData);
Path sourceBaseLocation = new Path("s3://source/");
Path replicaLocation = new Path("s3://target/foo/");
List<Path> sourceSubLocations = Lists.newArrayList(new Path(sourceBaseLocation, "year=2016"));
S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
Metrics metrics = s3s3Copier.copy();
assertThat(metrics.getBytesReplicated(), is(7L));
S3Object object = client.getObject("target", "foo/year=2016/data");
String data = IOUtils.toString(object.getObjectContent());
assertThat(data, is("bar foo"));
}
示例11: copyOneObjectPartitionedSourceBaseNested
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Test
public void copyOneObjectPartitionedSourceBaseNested() throws Exception {
client.putObject("source", "nested/year=2016/data", inputData);
Path sourceBaseLocation = new Path("s3://source/nested");// no slash at the end
Path replicaLocation = new Path("s3://target/foo/");
List<Path> sourceSubLocations = Lists.newArrayList(new Path(sourceBaseLocation, "year=2016"));
S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
s3s3Copier.copy();
S3Object object = client.getObject("target", "foo/year=2016/data");
String data = IOUtils.toString(object.getObjectContent());
assertThat(data, is("bar foo"));
}
示例12: copyOneObjectPartitionedHandlingS3ASchemes
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Test
public void copyOneObjectPartitionedHandlingS3ASchemes() throws Exception {
client.putObject("source", "year=2016/data", inputData);
Path sourceBaseLocation = new Path("s3a://source/");
Path replicaLocation = new Path("s3a://target/foo/");
List<Path> sourceSubLocations = Lists.newArrayList(new Path(sourceBaseLocation, "year=2016"));
S3S3Copier s3s3Copier = newS3S3Copier(sourceBaseLocation, sourceSubLocations, replicaLocation);
s3s3Copier.copy();
S3Object object = client.getObject("target", "foo/year=2016/data");
String data = IOUtils.toString(object.getObjectContent());
assertThat(data, is("bar foo"));
}
示例13: contentToString
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
private String contentToString(InputStream content, String idString) throws Exception {
try {
return IOUtils.toString(content);
} catch (Exception e) {
log.info(String.format("Unable to read input stream to string (%s)", idString), e);
throw e;
}
}
示例14: readResource
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
* Connects to the given endpoint to read the resource
* and returns the text contents.
*
* @param endpoint
* The service endpoint to connect to.
*
* @param retryPolicy
* The custom retry policy that determines whether a
* failed request should be retried or not.
*
* @return The text payload returned from the Amazon EC2 endpoint
* service for the specified resource path.
*
* @throws IOException
* If any problems were encountered while connecting to the
* service for the requested resource path.
* @throws SdkClientException
* If the requested service is not found.
*/
public String readResource(URI endpoint, CredentialsEndpointRetryPolicy retryPolicy) throws IOException {
int retriesAttempted = 0;
InputStream inputStream = null;
while (true) {
try {
HttpURLConnection connection = connectionUtils.connectToEndpoint(endpoint);
int statusCode = connection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
return IOUtils.toString(inputStream);
} else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) {
// This is to preserve existing behavior of EC2 Instance metadata service.
throw new SdkClientException("The requested metadata is not found at " + connection.getURL());
} else {
if (!retryPolicy.shouldRetry(retriesAttempted++, CredentialsEndpointRetryParameters.builder().withStatusCode(statusCode).build())) {
inputStream = connection.getErrorStream();
handleErrorResponse(inputStream, statusCode, connection.getResponseMessage());
}
}
} catch (IOException ioException) {
if (!retryPolicy.shouldRetry(retriesAttempted++, CredentialsEndpointRetryParameters.builder().withException(ioException).build())) {
throw ioException;
}
LOG.debug("An IOException occured when connecting to service endpoint: " + endpoint + "\n Retrying to connect again.");
} finally {
IOUtils.closeQuietly(inputStream, LOG);
}
}
}
示例15: DummyResponseServerBehavior
import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
public DummyResponseServerBehavior(HttpResponse response) {
this.response = response;
try {
this.content = IOUtils.toString(response.getEntity().getContent());
} catch (Exception e) {
}
}