无法将 Nativescript 图像上传到 S3
Unable to upload Nativescript images to S3
我有一个 nativescript-vue 应用程序,我想在其中拍摄相机拍摄的图像并上传到我的 S3 存储桶。
我安装了插件:
tns plugin add nativescript-aws-sdk
我的 app.js:
import { S3 } from "nativescript-aws-sdk/s3";
S3.init({ endPoint: '/images/person', accessKey: config.AWS_ACCESS_KEY, secretKey: config.AWS_SECRET_KEY, type: 'static' });
Vue.use(S3);
然后在我的函数中上传图片:
uploadPicture() {
console.log('uploadPicture called..');
const s = new S3();
console.log('S3 inited..' + JSON.stringify(s));
但这会导致:
CONSOLE LOG file:///node_modules/@nativescript/core/image-source/image-source.js:331:16: 'fromNativeSource() is deprecated. Use ImageSource constructor instead.'
CONSOLE LOG file:///app/components/Photo.vue:303:0: 'uploadPicture called..'
CONSOLE LOG file:///app/components/Photo.vue:304:0: 'S3 const..undefined'
所以 S3.init() 中的某些东西导致了错误。我没有在网上看到很多关于这个插件的例子,所以要么没有人对它有问题,要么它没有被使用?
任何能看出我做错了什么的人,你能给我指出正确的方向吗?
更新:
将导入从 vue 文件移动到 app.js:
import { S3 } from "nativescript-aws-sdk/s3";
S3.init({ endPoint: '/images/person', accessKey: config.AWS_ACCESS_KEY, secretKey: config.AWS_SECRET_KEY, type: 'static' });
在我的 Vue 文件中:
import { S3 } from 'nativescript-aws-sdk/s3';
const s3 = new S3();
const imageUploaderId = s3.createUpload({
file: that.cameraImage,
bucketName: config.AWS_BUCKET_NAME,
key: `ns_${isIOS ? 'ios' : 'android'}`+fileName,
acl: 'public-read',
completed: (error, success) => {
if (error) {
console.log(`S3 Download Failed :-> ${error.message}`);
}
if (success) {
console.log(`S3 Download Complete :-> ${success.path}`);
}
},
progress: progress => {
console.log(`Progress : ${progress.value}`);
}
}).catch((err) => {
console.error("createUpload() caught error: " + JSON.stringify(err));
});
s3.pause(imageUploaderId);
s3.resume(imageUploaderId);
s3.cancel(imageUploaderId);
但似乎什么也没发生 - 没有错误或进展。
您已声明 S3 两次,一次是全局声明:
import S3 from "nativescript-aws-sdk/s3";
我猜 uploadPicture() 方法中的本地版本:
const S3 = require('nativescript-aws-sdk/s3').S3;
import S3 必须执行初始化,而不是后面的:https://market.nativescript.org/plugins/nativescript-aws-sdk
我有一个 nativescript-vue 应用程序,我想在其中拍摄相机拍摄的图像并上传到我的 S3 存储桶。
我安装了插件:
tns plugin add nativescript-aws-sdk
我的 app.js:
import { S3 } from "nativescript-aws-sdk/s3";
S3.init({ endPoint: '/images/person', accessKey: config.AWS_ACCESS_KEY, secretKey: config.AWS_SECRET_KEY, type: 'static' });
Vue.use(S3);
然后在我的函数中上传图片:
uploadPicture() {
console.log('uploadPicture called..');
const s = new S3();
console.log('S3 inited..' + JSON.stringify(s));
但这会导致:
CONSOLE LOG file:///node_modules/@nativescript/core/image-source/image-source.js:331:16: 'fromNativeSource() is deprecated. Use ImageSource constructor instead.'
CONSOLE LOG file:///app/components/Photo.vue:303:0: 'uploadPicture called..'
CONSOLE LOG file:///app/components/Photo.vue:304:0: 'S3 const..undefined'
所以 S3.init() 中的某些东西导致了错误。我没有在网上看到很多关于这个插件的例子,所以要么没有人对它有问题,要么它没有被使用?
任何能看出我做错了什么的人,你能给我指出正确的方向吗?
更新: 将导入从 vue 文件移动到 app.js:
import { S3 } from "nativescript-aws-sdk/s3";
S3.init({ endPoint: '/images/person', accessKey: config.AWS_ACCESS_KEY, secretKey: config.AWS_SECRET_KEY, type: 'static' });
在我的 Vue 文件中:
import { S3 } from 'nativescript-aws-sdk/s3';
const s3 = new S3();
const imageUploaderId = s3.createUpload({
file: that.cameraImage,
bucketName: config.AWS_BUCKET_NAME,
key: `ns_${isIOS ? 'ios' : 'android'}`+fileName,
acl: 'public-read',
completed: (error, success) => {
if (error) {
console.log(`S3 Download Failed :-> ${error.message}`);
}
if (success) {
console.log(`S3 Download Complete :-> ${success.path}`);
}
},
progress: progress => {
console.log(`Progress : ${progress.value}`);
}
}).catch((err) => {
console.error("createUpload() caught error: " + JSON.stringify(err));
});
s3.pause(imageUploaderId);
s3.resume(imageUploaderId);
s3.cancel(imageUploaderId);
但似乎什么也没发生 - 没有错误或进展。
您已声明 S3 两次,一次是全局声明:
import S3 from "nativescript-aws-sdk/s3";
我猜 uploadPicture() 方法中的本地版本:
const S3 = require('nativescript-aws-sdk/s3').S3;
import S3 必须执行初始化,而不是后面的:https://market.nativescript.org/plugins/nativescript-aws-sdk