AWS S3上传卡在“等待”状态

时间:2018-07-13 14:52:53

标签: java android amazon-web-services amazon-s3 upload

我已经查看了有关此问题的几个问题和答案,但是仍然无法上传到S3存储桶。我已经在清单中的application标签中声明了该服务。

每个日志语句都返回正确的信息,但是状态始终为WAITING,并且不会触发任何传输侦听器。我没有收到任何错误,S3存储桶仍然为空。

我在做什么错了?

public void uploadStoredItems() {

    TransferUtility transferUtility = TransferUtility.builder()
            .context(AWSProvider.getInstance().getContext())
            .awsConfiguration(AWSProvider.getInstance().getConfiguration())
            .s3Client(new AmazonS3Client(AWSProvider.getInstance().getIdentityManager().getCredentialsProvider()))
            .build();

    try {
        for (File csvFile : csvFiles) {
            String filename = csvFile.getName(); //.substring(csvFile.getName().lastIndexOf('/') + 1);
            Log.d(TAG, "This is the filename for upload: " + filename);

            // FileReader reads text files in the default encoding.
            String line = null;
            FileReader fileReader = new FileReader(csvFile);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while ((line = bufferedReader.readLine()) != null) {
                Log.d(TAG, "Here are the lines: " + line);
            }

            // Always close files.
            bufferedReader.close();

            TransferObserver uploadObserver = transferUtility.upload(filename, csvFile);
            // Gets id of the transfer.
            Log.d(TAG, "This is the bucket: " + uploadObserver.getBucket());
            Log.d(TAG, "This is the state: " + uploadObserver.getState());
            Log.d(TAG, "This is the id: " + uploadObserver.getId());

            observers = transferUtility.getTransfersWithType(TransferType.UPLOAD);
            TransferListener listener = new TransferListener() {
                @Override
                public void onStateChanged(int id, TransferState state) {
                    Log.d(TAG, "onStateChanged: " + state.toString());
                    if (TransferState.COMPLETED == state) {
                        Log.d(TAG, "COMPLETE: " + state.toString());
                    }
                }

                @Override
                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                    float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;
                    int percentDone = (int) percentDonef;

                    Log.d(TAG, "ID:" + id + " bytesCurrent: " + bytesCurrent
                            + " bytesTotal: " + bytesTotal + " " + percentDone + "%");
                }

                @Override
                public void onError(int id, Exception ex) {
                    // Handle errors
                    Log.d(TAG, "ERROR ID:" + id + " " + ex.getMessage());
                }
            };

            for (TransferObserver observer : observers) {

                if (TransferState.WAITING.equals(observer.getState())
                        || TransferState.WAITING_FOR_NETWORK.equals(observer.getState())
                        || TransferState.IN_PROGRESS.equals(observer.getState())) {
                    observer.setTransferListener(listener);
                }

                Log.d(TAG, "\n observers - id: " + observer.getId() + " state: " + observer.getState() + " key: " + observer.getKey() + " bytes total: " + observer.getBytesTotal());
            }
            Log.d(TAG, "Bytes Total: " + uploadObserver.getBytesTotal());

        }
        csvFiles.clear();
    } catch(java.io.FileNotFoundException ex) {
        System.out.println("Unable to open file");
    } catch(java.io.IOException ex) {
        System.out.println("Error reading file");
    }
} 

1 个答案:

答案 0 :(得分:0)

结果是,我必须声明TransferUtility服务以与我调用它的服务正在运行的过程相同的方式运行:

<application

    ...

    <service
      android:name=".MainService"
      android:enabled="true"
      android:process=":main_service"
      android:stopWithTask="false" />
    <service
      android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
      android:process=":main_service"
      android:enabled="true" />

    ...

</application>