简体   繁体   English

Amazon S3上传图片 - 直接从浏览器使用角度js

[英]Amazon S3 upload image - using angular js directly from browser

I am trying to upload images from my browser to Amazon S3 directly, using angular js. 我正在尝试使用角度js将图像从我的浏览器直接上传到Amazon S3。

Below is my code. 以下是我的代码。

function _upload($files) {
                $scope.file = $files[0];
                $scope.creds = {
                    access_key: '***',
                    secret_key: '***',
                    bucket: 'sabari-test'
                };

                var bucket = new AWS.S3({
                    params: {
                        Bucket: $scope.creds.bucket
                    }
                });
                AWS.config.accessKeyId = $scope.creds.access_key;
                AWS.config.secretAccessKey = $scope.creds.secret_key;
                AWS.config.region = 'us-west-2';

                // AWS.

                if ($scope.file) {
                    // Perform File Size Check First
                    var fileSize = Math.round(parseInt($scope.file.size));
                    if (fileSize > $scope.sizeLimit) {
                        console.log('Sorry, your attachment is too big.');
                        return false;
                    }
                    // Prepend Unique String To Prevent Overwrites
                    var uniqueFileName = 'hai' + '-' + $scope.file.name;

                    var params = {
                        Key: uniqueFileName,
                        ContentType: $scope.file.type,
                        Body: $scope.file,
                        ServerSideEncryption: 'AES256'
                    };

                    bucket.putObject(params, function(err, data) {
                        if (err) {
                            console.log(err.message);
                            return false;
                        } else {
                            // Upload Successfully Finished
                            console.log('File Uploaded Successfully');
                        }
                    })
                } else {
                    // No File Selected
                    console.log('Please select a file to upload');
                }
            }

I get the below error: 我得到以下错误:

"Missing credentials in config" “配置中缺少凭据”

Please let me know what is the missing credential? 请告诉我丢失的凭证是什么?

Thanks. 谢谢。

You need to replace these lines: 你需要替换这些行:

            var bucket = new AWS.S3({
                params: {
                    Bucket: $scope.creds.bucket
                }
            });
            AWS.config.accessKeyId = $scope.creds.access_key;
            AWS.config.secretAccessKey = $scope.creds.secret_key;
            AWS.config.region = 'us-west-2';

With this: 有了这个:

            var bucket = new AWS.S3({
                region = 'us-west-2',
                credentials: new AWS.Credentials($scope.creds.access_key, $scope.creds.secret_key)
            });

And then move the Bucket to your var params 然后将Bucket移动到var params

                var params = {
                    Bucket: $scope.creds.bucket,
                    Key: uniqueFileName,
                    ContentType: $scope.file.type,
                    Body: $scope.file,
                    ServerSideEncryption: 'AES256'
                };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 直接从浏览器将图像上传到Amazon S3-使用angular js - Upload image to amazon s3 directly from browser - using angular js Amazon S3上传图像-跟踪上传进度-Angular JS - Amazon S3 upload image - track the Upload progress - Angular JS 如何从浏览器下载图像并上传到Amazon S3 - How to download Image from browser & upload to Amazon S3 网络摄像头js将图像上传到Amazon S3 - Webcam js upload image to Amazon S3 使用Angular JS从Amazon S3专用存储桶检索图像 - Retrieve image from Amazon S3 private bucket using Angular JS 尝试使用 fetch 从浏览器通过 signedUrl 上传图像 S3,但图像在 s3 中为空白 - Trying to upload an image S3 via a signedUrl from a browser using fetch but the image is blank in s3 是否可以从浏览器上传亚马逊s3上的流? - Is it possible to upload stream on amazon s3 from browser? 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? 如何使用Express JS 3.X和Angle JS将图像流式传输到Amazon S3? - How to stream image to amazon s3 using express js 3.X and angular js? 如何使用Amazon Cognito用户池令牌将文件直接上传到Amazon S3 - How to upload a file directly to amazon S3 using Amazon Cognito user pool tokens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM