Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js s3预先签名的url帖子说bucket名称不正确_Node.js_Angular_Amazon Web Services_Amazon S3 - Fatal编程技术网

Node.js s3预先签名的url帖子说bucket名称不正确

Node.js s3预先签名的url帖子说bucket名称不正确,node.js,angular,amazon-web-services,amazon-s3,Node.js,Angular,Amazon Web Services,Amazon S3,我遵循这一点,我将src中的url切换到返回的预签名url,但我一直得到无效的bucket名称,我不知道为什么。我想这就是教程所说的 注意:别忘了更改图像源的url以获得您的bucket名称。 我做了一个{this.presignedURl}},它确实将url输出到页面,因此它的分配肯定是正确的,但我仍然得到了错误。我做错了什么 错误: 对象{头:{…},状态:400,状态文本:“错误请求”,url:http://localhost:3000/api/v1/upload,ok:false,名称:

我遵循这一点,我将src中的url切换到返回的预签名url,但我一直得到无效的bucket名称,我不知道为什么。我想这就是教程所说的

注意:别忘了更改图像源的url以获得您的bucket名称。

我做了一个{this.presignedURl}},它确实将url输出到页面,因此它的分配肯定是正确的,但我仍然得到了错误。我做错了什么

错误:

对象{头:{…},状态:400,状态文本:“错误请求”,url:http://localhost:3000/api/v1/upload,ok:false,名称:“HttpErrorResponse”,消息:“Http失败响应http://localhost:3000/api/v1/upload: 400错误请求”,错误:“\n\n\n\n错误\n\n\n
组件技术

  <input (change)="onImagePicked($event)" placeholder="Upload Image" type="file" />
  <button (click)="onImageUpload()">Upload Image</button>
   {{this.presignedURl}}
  <div *ngIf="imageUrl">
    Preview Image from AWS
    <br />
    <img width="200px" src='{{this.presignedUrl}}' /></div>
html


上传图像
{{this.presignedURl}}
从AWS预览图像


http://localhost:3000
放
3000
*
http://localhost:3000
得到
3000
*

我看了教程,它似乎没有提供一个预先签名的url。方法基本上是上传一个具有
公共读取
权限的图像,并使用s3端点读取图像

根据教程,您应该将bucket url后跟路径

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
如果要生成预签名的URL, 您还可以轻松地使用lambda而不是使用express后端来生成预签名URL


您还可以使用
putObject
生成一个预签名的url,用于上载目的。

我将代码更改为bucket,但现在它显示403禁止访问被拒绝。我根据教程创建了IAM用户,并且我正在使用该IAM用户的secretAccessKey和accessKeyId。我需要对bucket策略或其他方面做些什么吗还有?您只需要使用IAM凭据进行后端上传。上传是否正常?您确实需要在bucket上设置cros设置。您完成了吗?我在上面添加了cors配置。此外,如果我将src更改为只说bucket,我不确定如何指向预签名。我意识到教程没有这样做谈论预签名url,但这是我一直试图理解的东西,并与此解决方案结合在一起。如果它是预签名url,它将已经有bucket url和签名。如果您的预签名url是“GetObject”“,您可以直接将其添加到img标记的
src
。您还可以为
PutObject
生成预签名url,该url可用于直接上载到S3,而无需通过后端API。生成预签名url仍需要后端API。”
  <input (change)="onImagePicked($event)" placeholder="Upload Image" type="file" />
  <button (click)="onImageUpload()">Upload Image</button>
   {{this.presignedURl}}
  <div *ngIf="imageUrl">
    Preview Image from AWS
    <br />
    <img width="200px" src='{{this.presignedUrl}}' /></div>
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>http://localhost:3000</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
    <AllowedOrigin>http://localhost:3000</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

<img width="200px" src="https://my-upload-example-bucket.s3.amazonaws.com/{{ 
     imageUrl }}" 
/>
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
import AWS = require('aws-sdk');
const S3_BUCKET = 'bucket-to-presign-example'


const getpresignedUrl = async (fileName, fileType) => {
  const s3 = new AWS.S3();  // Create a new instance of S3

  // Set up the payload of what we are sending to the S3 api
  const s3Params = {
    Bucket: S3_BUCKET,
    Key: fileName,
    Expires: 5000,
  };

  const presignedURL = await s3.getSignedUrlPromise('getObject', s3Params);
  return presignedURL;
}