te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - AWS S3 Metadata Not Retrievable After Upload Using @aws-sdkclient-s3 - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AWS S3 Metadata Not Retrievable After Upload Using @aws-sdkclient-s3 - Stack Overflow

programmeradmin3浏览0评论

I'm encountering an issue where custom metadata set during file upload to AWS S3 using @aws-sdk/client-s3 is not being retrieved when fetching the object's metadata later.

Problem Description: I'm uploading media files (images, videos) to an S3 bucket along with custom metadata (title and description) using the PutObjectCommand. The upload appears to be successful, and I can see the files in my bucket. However, when I attempt to retrieve the metadata using HeadObjectCommand, the Metadata property of the response is consistently empty.

import { S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';

const s3Client = new S3Client({
  region: 'MyRegion',
  credentials: {
    accessKeyId: 'MyAccessKey',
    secretAccessKey: 'MySecretAccessKey',
  },
});

export const BUCKET_BASE_URL = "MyBucketBaseURL";

export const getHrefAgainstKey = (key) => {
  return `${BUCKET_BASE_URL}${key}`;
};

const S3MediaUploader = () => {
  const [file, setFile] = useState(null);
  const [metadata, setMetadata] = useState({ title: '', description: '' });
  const [uploadedMedia, setUploadedMedia] = useState([]);
  const [loading, setLoading] = useState(false);
  const bucketName = 'MyBucketName';

  const handleFileChange = (e) => {
    setFile(e.target.files[0]);
  };

  const handleMetadataChange = (e) => {
    setMetadata({ ...metadata, [e.target.name]: e.target.value });
  };

  const handleUpload = async () => {
    if (!file) {
      alert('Please select a file.');
      return;
    }

    setLoading(true);

    const reader = new FileReader();

    reader.onload = async (event) => {
      const arrayBuffer = event.target.result;
      const uint8Array = new Uint8Array(arrayBuffer);

      const params = {
        Bucket: bucketName,
        Key: file.name,
        Body: uint8Array,
        Metadata: {
          title: metadata.title.toLowerCase(),
          description: metadata.description.toLowerCase(),
        },
        ACL: "public-read",
        ContentType: file.type,
      };

      try {
        const response = await s3Client.send(new PutObjectCommand(params));
        alert('File uploaded successfully!');
        setFile(null);
        setMetadata({ title: '', description: '' });
        fetchMedia();
      } catch (error) {
        console.error('Error uploading file:', error);
        alert('Error uploading file.');
      } finally {
        setLoading(false);
      }
    };

    reader.onerror = () => {
      console.error('Error reading file.');
      setLoading(false);
    };

    reader.readAsArrayBuffer(file);
  };

  const fetchMedia = async () => {
    setLoading(true);
    try {
      const response = await s3Client.send(new ListObjectsV2Command({ Bucket: bucketName }));
      if (response.Contents) {
        const media = await Promise.all(
          response.Contents.map(async (item) => {
            const getObjectParams = {
              Bucket: bucketName,
              Key: item.Key,
            };

            const //Empty Metadata = await s3Client.send(new HeadObjectCommand(getObjectParams));   **// Getting Empty Metadata.Metadata**
            return {
              key: item.Key,
              href: getHrefAgainstKey(item.Key),
              url: BUCKET_BASE_URL,
              metadata: {metadataResponse.Metadata},
            };
          })
        );
        setUploadedMedia(media);
      } else {
        setUploadedMedia([]);
      }
    } catch (error) {
      console.error('Error fetching media:', error);
    } finally {
      setLoading(false);
    }
  };

IAM User Policy:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:*",
        "s3tables:*",
        "iam:PassRole"
      ],
      "Resource": "*"
    }
  ]
}

Dependencies:

"dependencies": {
  "@aws-sdk/client-s3": "^3.749.0",
}

Troubleshooting Steps Taken: Verified that the upload process completes without errors. Confirmed that the files are present in the S3 bucket. Tried using both HeadObjectCommand and GetObjectCommand to fetch metadata. Checked IAM user permissions to ensure full S3 access. Ensured that the metadata keys are lowercase (as per S3 requirements).

Expected Behavior: I expect the Metadata property of the HeadObjectCommand or GetObjectCommand response to contain the title and description values set during upload.

Actual Behavior: The Metadata property is consistently empty.

发布评论

评论列表(0)

  1. 暂无评论