无法将Nativescript图像上传到S3

编程入门 行业动态 更新时间:2024-10-11 19:17:25
本文介绍了无法将Nativescript图像上传到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必须执行初始化,而不是以后的初始化: market.nativescript/plugins/nativescript-aws-sdk

The import S3 has to do the init, not the later as per: market.nativescript/plugins/nativescript-aws-sdk

更多推荐

无法将Nativescript图像上传到S3

本文发布于:2023-11-24 11:04:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Nativescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!