为什么当我将图像上传到S3存储桶时,我的API无法正常工作?

时间:2019-05-30 12:03:27

标签: .net amazon-s3 core nuxt

我将Vue / NuxtJs用作前端,并将.net core 2.1用作后端。我需要将图像发送到后端,然后上传到S3存储桶。它在localhost中很好用,但是,例如,当我发布API开始报错时,例如文件变大并损坏,并且仅在图像中发生。有人可以帮忙吗?

前端

async onUpload() {
      this.isNew = true;
      this.showProgess = true;
      debugger;
      const file = new FormData();
      console.log(this.selectedFile.size);
      file.append("file", this.selectedFile, this.selectedFile.name);
      file.append("fileType", 0);
      await this.$axios
        .$post(process.env.API_URL + `/PlanImages`, file, {
          headers: {
            "Content-Type": "multipart/form-data"
          },
          onUploadProgress: function (progressEvent) {
            this.uploadPercentage = parseInt(
              Math.round((progressEvent.loaded * 100) / progressEvent.total)
            );
          }
        })
        .then((response) => {
          if (response.result.success) {
            this.uploadSuccess = true;
            this.saveDisable = false;
            this.uploadDisable = true;
            this.editedItem.fileUrl = response.result.fileURL;
            console.log(response);
          }
          this.showProgess = false;
          console.log("call one");
        })
        .catch((error) => {
          this.showProgess = false;
          console.log(error);
        });
    },

后端

 public async Task<UploadPhotoModel> UploadPhoto(IFormFile files, string bucket)
        {
            var test = files.Length;
            // connecting to the client
            var client = new AmazonS3Client("accessKey", "accessSecret", Amazon.RegionEndpoint.CACentral1);

            var fileName = Guid.NewGuid().ToString() + files.FileName;


            using (var newMemoryStream = new MemoryStream())
            {
                files.CopyTo(newMemoryStream);

                var uploadRequest = new TransferUtilityUploadRequest
                {
                    InputStream = newMemoryStream,
                    Key = fileName,
                    BucketName = "bucket",
                    PartSize = 6291456, // 6 MB.
                    CannedACL = S3CannedACL.PublicRead
                };

                var fileTransferUtility = new TransferUtility(client);
                await fileTransferUtility.UploadAsync(uploadRequest);
            }


0 个答案:

没有答案