智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述Android Amazon S3文件上传不起作用相关的知识,希望能为你提供帮助。
我正在尝试将sd卡上的文件上传到我的amazon s3存储桶。这是班级:
public class FileUploadDownload
{
Context context;
boolean transferComplete = false;
float transferPercentage = 0;
public FileUploadDownload(Context context)
{
this.context = context;
}public void uploadFile(File file , String nameOfFile)
{
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
context,
"cognito_identity_pool_id", // Identity Pool ID
Regions.EU_WEST_1 // Region
);
AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
TransferUtility transferUtility = new TransferUtility(s3, context);
TransferObserver observer = transferUtility.upload(
"s3_bucket",/* The bucket to upload to */
nameOfFile,/* The key for the uploaded object */
file/* The file where the data to upload exists */
);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
Toast.makeText(context,"State changed to : "+state.toString(),Toast.LENGTH_SHORT).show();
}@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {//transferPercentage = (float)bytesCurrent/(float)bytesTotal;
if(bytesCurrent == bytesTotal)
{
//transferComplete = true;
Toast.makeText(context,"Upload completed!",Toast.LENGTH_SHORT).show();
}
}@Override
public void onError(int id, Exception ex) {
Toast.makeText(context,"Upload Unsuccessful due to `"+ex.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
这是我在Activity的OnCreate()方法中实现的代码片段:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/EpisodeRecorder/User1/Episode1/User1e1p1.jpg");
FileUploadDownload fud = new FileUploadDownload(this);
fud.uploadFile(dir,"trialFileUpload.jpg");
这是清单的顶部:
<
?xml version="1.0" encoding="utf-8"?>
【Android Amazon S3文件上传不起作用】
<
uses-permission android:name="android.permission.CAMERA" />
<
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<
uses-permission android:name="android.permission.RECORD_AUDIO" />
<
uses-permission android:name="android.permission.INTERNET" />
<
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<
application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
<
service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:enabled="true" />
.....
我通过toasts显示所有调试消息。最初它显示“IN PROGRESS”,稍后,它显示“FAILED”错误消息“java.lang.IllegalStateException”在DEBUG LOG中它说:
10-12 21:01:01.1381252-14041/? E/CloudSettingsConnection﹕ Connection failed : SERVER_ERROR
我无法弄清楚发生了什么。如果你能指出错误那就太棒了。或者,如果您能够确定错误是在Amazon s3 / cognito / IAM设置还是代码中,我会很高兴。谢谢!
这是要求的整个堆栈跟踪:
javax.net.ssl.SSLException: Write error: ssl=0x60970398: I/O error during system call, Connection reset by peer
at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:731)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:131)
at com.android.okhttp.internal.http.HttpTransport$FixedLengthOutputStream.write(HttpTransport.java:236)
at com.amazonaws.http.UrlHttpClient.write(UrlHttpClient.java:172)
at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:129)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:353)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4204)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1618)
at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.uploadSinglePartAndWaitForCompletion(UploadTask.java:174)
at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:74)
at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:40)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
10-13 00:37:56.42714450-14483/? E/UploadTask﹕ Unable to execute HTTP request: Write error: ssl=0x60970398: I/O error during system call, Connection reset by peer
10-13 00:37:56.49714450-14450/? D/UPLOAD FAIL﹕ java.lang.IllegalStateException: Transfer failed.
答案我不确定这对你有用,但我遇到了同样的问题,我发现该文件的名称最后有“.png”,所以我删除它并且它有效,在删除之前我检查currentBytes,在它给我FAILED状态之前,currentBytes的数量等于totalBytes但是我的状态仍为FAILED,所以
尝试删除fileName中的任何扩展名我希望这有帮助
推荐阅读
- 带有“ 307临时重定向”的Android VideoView播放URL
- BLE在Android上没有绑定的配对
- 使用create-react-app的其他HtmlWebpackPlugin()页面
- Flutter Notification OneSignal-无法确定任务'(app:compileDebugJavaWithJavac'的依赖项)
- 用于App Service的地形名称“ xxx”必须是全局唯一的,并且不可用”
- 像webapp.to一样开发移动应用程序
- Android(未加载测试广告)
- 如何将我在Android Studio中制作的应用程序放入手机中()
- Android jetpack生物识别DeviceCredentialHandlerActivity输出=真的需要吗()