我在我的应用程序中实际执行的操作是捕获图像,将其保存到磁盘然后将其上载到s3。
我要上传的代码是
public void credentialsProvider(){
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"ap-south-1:---------------", // Identity Pool ID
Regions.AP_SOUTH_1 // Region
);
setAmazonS3Client(credentialsProvider);
}
/**
* Create a AmazonS3Client constructor and pass the credentialsProvider.
* @param credentialsProvider
*/
public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider){
// Create an S3 client
s3 = new AmazonS3Client(credentialsProvider);
// Set the region of your S3 bucket
s3.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
}
public void setTransferUtility(){
transferUtility = new TransferUtility(s3, getApplicationContext());
}
/**
* This method is used to upload the file to S3 by using TransferUtility class
*
*/
public void fileUpload(){
TransferObserver transferObserver = transferUtility.upload(
"train-faces", /* The bucket to upload to */
time1+date1+"_"+"1" + "_pic.jpg", /* The key for the uploaded object */
fileToUpload /* The file where the data to upload exists */
);
transferObserverListener(transferObserver);
}
/**
* This is listener method of the TransferObserver
* Within this listener method, we get status of uploading and downloading file,
* to display percentage of the part of file to be uploaded or downloaded to S3
* It displays an error, when there is a problem in uploading or downloading file to or from S3.
* @param transferObserver
*/
public void transferObserverListener(TransferObserver transferObserver){
transferObserver.setTransferListener(new TransferListener(){
@Override
public void onStateChanged(int id, TransferState state) {
if (state.COMPLETED.equals(transferObserver.getState())) {
Toast.makeText(CameraService.this, "File Upload Complete", Toast.LENGTH_SHORT).show();
fileToUpload.delete();
}
Log.e(TAG, "statechange"+state+"");
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
int percentage = (int) (bytesCurrent/bytesTotal * 100);
Log.e(TAG,"percentage"+percentage +"");
if (percentage==100){
//fileToUpload.delete();
}
}
@Override
public void onError(int id, Exception ex) {
Log.e(TAG,"error"+"error");
}
});
}
保存图像时,我只需调用fileUpload(),有时图像会成功上传,有时会得到: 由于无法执行Http请求7无法解析主机没有与主机名关联的地址
,因此无法上传我想让它更可靠,并避免在我的应用程序中如何实现这一点的上传失败。
答案 0 :(得分:0)
我没有遇到7 unable to resolve host no address associated with hostname
错误,但这通常发生在存在网络问题时,如Hemant所述。
关于读取超时错误Failed to upload: 196 due to Unable to execute HTTP request: Read timed out
,这可能是也可能不是网络问题。当然,糟糕的网络肯定会导致这种情况,但TransferUtility
存在已知问题,即使没有网络问题,也会显示某些上传失败。
如果是真正的网络问题,按建议增加套接字超时可能会修复/减少错误。
ClientConfiguration cc = new ClientConfiguration();
cc.setSocketTimeout(120000); AmazonS3 s3 = new AmazonS3Client(credentialsProvider, cc);
根据您的偏好将超时设置为某个高值。如果这不能解决或减少错误&您确定没有网络问题,这可能是TransferUtility
本身的问题。用户提到直接上传大文件会导致此错误,并且一个人使用的解决方法是在上传之前上传1 Kb虚拟文件,但这显然不是解决方案。有关详细信息,请参阅this post。我个人仅在网络不良时遇到此错误,因此我无法验证解决方法是否有效以及该问题是否已在SDK的更高版本中得到修复。
答案 1 :(得分:0)
当互联网不稳定时,我遇到了同样的问题,但是在进行配置更改后,它仍然可以工作。
val configuration = ClientConfiguration()
configuration.maxErrorRetry = 10
configuration.maxConnections = 100
configuration.connectionTimeout = 0
configuration.socketTimeout = 0
val s3Client = AmazonS3Client(credentials,configuration)