无法将Nativescript图像上传到S3 [英] Unable to upload Nativescript images to S3
问题描述
我有一个nativescript-vue应用程序,我想在其中拍摄相机拍摄的图像并将其上传到我的S3存储桶中.
I have a nativescript-vue application where I want to take a camera taken image and upload to my S3 bucket.
我安装了插件:
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);
然后在我的函数中上传图片:
then in my function to upload an image:
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()引起错误.我在网上没有看到很多此插件的示例,因此没有人遇到问题或未使用它?
So there is something with the S3.init() that is causing an error. I do not see many examples of this plugin online so either nobody has issues with it or its not being used?
任何人都可以看到我在做错什么,您能向我指出正确的方向吗?
Anyone who can see what I am doing wrong, can you kindly point me in the right direction?
更新:将导入从vue文件移动到app.js:
Update: Moved the import from vue file to 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);
但是似乎什么也没发生-没有错误或进展.
But nothing seems to happen - no errors or progress.
推荐答案
您已两次声明S3,并且在全局范围内声明过一次:
You've declared S3 twice, once globally:
import S3 from "nativescript-aws-sdk/s3";
我猜到uploadPicture()方法中的本地版本:
and I'm guessing a local version inside the uploadPicture() method:
const S3 = require('nativescript-aws-sdk/s3').S3;
导入S3必须执行初始化,而不是以后的初始化: https://market.nativescript.org/plugins/nativescript-aws-sdk
The import S3 has to do the init, not the later as per: https://market.nativescript.org/plugins/nativescript-aws-sdk
这篇关于无法将Nativescript图像上传到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!