1
我期待開發一個Singletone AmazonS3Client,它可以爲我的應用程序上傳文件到Amazon S3服務器。但是,我找不到如何檢查連接是否處於活動狀態並能夠上傳存儲桶。如何檢查AmazonS3Client連接是否處於活動狀態
是否存在特定的異常,如果連接斷開會引發異常?或者連接在一段時間後掉線?
任何答案都會有所幫助。
這裏是我的代碼片段:
private static final AmazonS3Client s3Client;
static {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
s3Client = new AmazonS3Client(awsCredentials);
}
private static boolean writeFile(AmazonS3Client s3Client, String fileName, File file, Boolean publicRead){
try {
PutObjectRequest p = new PutObjectRequest(bucketName, fileName, file);
if (publicRead)
p.setCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(p);
System.out.println("URL: " + S3_BASE_URL + fileName);
return true;
} catch (AmazonServiceException ase){
ase.printStackTrace();
} catch (AmazonClientException ace) {
ace.printStackTrace();
} catch (Exception ex){
ex.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}