Android Amazon S3文件上传不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"?>

<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.138   1252-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.427  14450-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.497  14450-14450/? D/UPLOAD FAIL﹕ java.lang.IllegalStateException: Transfer failed.
答案

我不确定这对你有用,但我遇到了同样的问题,我发现该文件的名称最后有“.png”,所以我删除它并且它有效,在删除之前我检查currentBytes,在它给我FAILED状态之前,currentBytes的数量等于totalBytes但是我的状态仍为FAILED,所以

尝试删除fileName中的任何扩展名

我希望这有帮助

以上是关于Android Amazon S3文件上传不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用 SDK 的情况下将文件从 Android 上传到 Amazon S3

指向 Amazon S3 存储桶的子域在英国不起作用

将图像从 Android 上传到 Amazon S3?

Emrfs 文件与 s3 同步不起作用

AWS S3使用邮递员将文件上传到存储桶不起作用

Amazon S3 上传问题 Android SDK、com.amazonaws.AmazonClientException:读取的数据 (4567265) 多于预期 (4561427)