相关文章推荐
重情义的自行车  ·  在AWS ...·  5 天前    · 
有情有义的汤圆  ·  Vue + webpack + ...·  9 月前    · 
害羞的针织衫  ·  Highlighting more ...·  10 月前    · 
安静的椅子  ·  Oracle ...·  1 年前    · 

4. npm更新命令:npm-check 检查更新 npm-upgrade更新,保证正在运行至少运行Node.js的版本大于8.11+,NPM版本大于5.x

**2.开始接入**

- 先依赖一下所需要的架包

//AWS

implementation 'com.amazonaws:aws-android-sdk-s3:2.12.+'

implementation('com.amazonaws:aws-android-sdk-mobile-client:2.12.+@aar') { transitive = true }

implementation('com.amazonaws:aws-android-sdk-auth-userpools:2.12.+@aar') { transitive = true }

implementation 'org.testng:testng:6.9.6'

//Base SDK

implementation 'com.amazonaws:aws-android-sdk-core:2.12.+'

//AppSync SDK

implementation 'com.amazonaws:aws-android-sdk-appsync:2.7.+'

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'

implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

- 在清单文件里添加

<service android:name="org.eclipse.paho.android.service.MqttService" />

<service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" />

- 输入命令  `npm install -g @aws-amplify/cli`

**3.代码部分**

10. 由于从2.7.0版开始TransferService将不会自动启动或停止TransferUtility。所以你必须在你的`Application`里面添加以下代码去启动TransferService服务。

//AWS 上传

mAWSAppSyncClient = AWSAppSyncClient.builder()

.context(getApplicationContext())

.awsConfiguration(new AWSConfiguration(getApplicationContext()))

.build();

getApplicationContext().startService(new Intent(getApplicationContext(), TransferService.class));

- 上传文件代码,我这里上传一张图片作为案例。Constants类里填好自己的Key和密钥

private static String IMAGE_DATA_NAME = "public/user2"+DateUtil.getYear()+""+DateUtil.getMonth()+""+DateUtil.getDay()+""+ RandomUntil.getNewRandomCode(6)+".jpg";

//上传头像地址(用于下次加载图片)

private static String PAHT =  Constants.AMAZON_S3_PIC + IMAGE_DATA_NAME;

* 上传到AWS

private void AwsTest() {

AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {

@Override

public void onResult(UserStateDetails userStateDetails) {

Log.i(TAG, "AWSMobileClient initialized. User State is " + userStateDetails.getUserState());

@Override

public void onError(Exception e) {

Log.e(TAG, "Initialization error.", e);

uploadWithTransferUtility();

public void uploadWithTransferUtility() {

AWSCredentials credentials = new BasicAWSCredentials(Constants.AMAZON_S3_KEY, Constants.AMAZON_S3_KEY_PWD);

TransferUtility transferUtility =

TransferUtility.builder()

.context(getApplicationContext())

.awsConfiguration(AWSMobileClient.getInstance().getConfiguration())

.s3Client(new AmazonS3Client(credentials))

.build();

TransferObserver uploadObserver =

transferUtility.upload(

IMAGE_DATA_NAME,

new File(mImageCropPath));

// Attach a listener to the observer to get state update and progress notifications

uploadObserver.setTransferListener(new TransferListener() {

@Override

public void onStateChanged(int id, TransferState state) {

if (TransferState.COMPLETED == state) {

// Handle a completed upload.

LogUtil.d(TAG,"AWS上传图片:"+IMAGE_DATA_NAME+"成功");

@Override

public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100;

int percentDone = (int)percentDonef;

Log.d("YourActivity", "ID:" + id + " bytesCurrent: " + bytesCurrent

+ " bytesTotal: " + bytesTotal + " " + percentDone + "%");

@Override

public void onError(int id, Exception ex) {

// Handle errors

LogUtil.d(TAG,"AWS上传图片:"+IMAGE_DATA_NAME+"失败");

PAHT = "";

// If you prefer to poll for the data, instead of attaching a

// listener, check for the state and progress in the observer.

if (TransferState.COMPLETED == uploadObserver.getState()) {

// Handle a completed upload.

Log.d("YourActivity", "Bytes Transferred: " + uploadObserver.getBytesTransferred());

Log.d("YourActivity", "Bytes Total: " + uploadObserver.getBytesTotal());

- 在你需要下载的`Activity`的`onCreat`方法中添加

```

* AWS下载

private void AwsDownld() {

AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {

@Override

public void onResult(UserStateDetails userStateDetails) {

Log.i(TAG, "AWSMobileClient initialized. User State is " + userStateDetails.getUserState());

@Override

public void onError(Exception e) {

Log.e(TAG, "Initialization error.", e);

downloadWithTransferUtility();

private void downloadWithTransferUtility() {

TransferUtility transferUtility =

TransferUtility.builder()

.context(getApplicationContext())

.awsConfiguration(AWSMobileClient.getInstance().getConfiguration())

.s3Client(new AmazonS3Client(AWSMobileClient.getInstance()))

.build();

TransferObserver downloadObserver =

transferUtility.download(

"public/s3Key.txt",

new File("/path/to/file/localFile.txt"));

// Attach a listener to the observer to get state update and progress notifications

downloadObserver.setTransferListener(new TransferListener() {

@Override

public void onStateChanged(int id, TransferState state) {

if (TransferState.COMPLETED == state) {

// Handle a completed upload.

@Override

public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

float percentDonef = ((float)bytesCurrent/(float)bytesTotal) * 100;

int percentDone = (int)percentDonef;

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

@Override

public void onError(int id, Exception ex) {

// Handle errors

// If you prefer to poll for the data, instead of attaching a

// listener, check for the state and progress in the observer.

if (TransferState.COMPLETED == downloadObserver.getState()) {

// Handle a completed upload.

Log.d("Your Activity", "Bytes Transferred: " + downloadObserver.getBytesTransferred());

Log.d("Your Activity", "Bytes Total: " + downloadObserver.getBytesTotal());

- 最后上传成功后就可以在Amazon S3控制台的桶里查看了