在javascript中上载文件时,如何以编程方式设置AWSS3对象元数据?

5cg8jx4n  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(445)

在javascript中上载文件时,如何以编程方式设置aws s3对象元数据?
我想为每个文件设置内容类型和内容处置标题,这样以后就不必手动更改标题。
当我尝试下面的代码时,我得到“uncaught(in promise)typeerror:无法读取未定义的属性'add'。
如果我取出中间件代码,文件上传成功,但我必须通过s3控制台手动设置元数据。
任何协助都将不胜感激。

var upload = new AWS.S3.ManagedUpload({
    params: {
      Bucket: projectBucketName,
      Key: jobKey,
      Body: file
    }
    //tags: [{ Key: 'Content-Type', Value: 'application/pdf' }, { Key: 'Content-Disposition', Value: 'inline' }]
  });

  upload.middlewareStack.add(
  (next, context) => async (args) => {
    args.request.headers["Content-Type"] = "application/pdf";
    const result = next(args);
    // result.response contains data returned from next middleware.
    return result;
  },
  {
    step: "build",
    name: "addContentTypeMetadataMiddleware",
    tags: ["METADATA", "CONTENTTYPE"],
  }
);

  upload.middlewareStack.add(
  (next, context) => async (args) => {
    args.request.headers["Content-Disposition"] = "inline";
    const result = next(args);
    // result.response contains data returned from next middleware.
    return result;
  },
  {
    step: "build",
    name: "addContentDispositionMetadataMiddleware",
    tags: ["METADATA", "CONTENTDISPOSITION"],
  }
);

   promise = await upload.promise();
brgchamk

brgchamk1#

要在onject上设置元数据,可以使用s3api。下面是一个用java实现的示例。您可以将其移植到用于javascript的aws sdk。

public static String putS3Object(S3Client s3,
                                     String bucketName,
                                     String objectKey,
                                     String objectPath) {

        try {

           // Define the metadata
            Map<String, String> metadata = new HashMap<>();
            metadata.put("author", "Mary Doe");
            metadata.put("version", "1.0.0.0");

            PutObjectRequest putOb = PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .metadata(metadata)
                    .build();

            PutObjectResponse response = s3.putObject(putOb,
                    RequestBody.fromBytes(getObjectFile(objectPath)));

            return response.eTag();

        } catch (S3Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }

    // Return a byte array
    private static byte[] getObjectFile(String filePath) {

        FileInputStream fileInputStream = null;
        byte[] bytesArray = null;

        try {
            File file = new File(filePath);
            bytesArray = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesArray);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bytesArray;
    }

相关问题