返回 Skill 列表
extension
分类: 开发与工程无需 API Key

AWS S3 Storage Operations

AWS SDK v3 S3客户端操作,用于带有凭证管理和错误处理的视频上传。在向S3上传视频、配置S3客户端、处理上传失败或为对象存储操作实现重试逻辑时使用。

person作者: jakexiaohubgithub

AWS S3 Storage Operations

This Skill provides Claude Code with specific guidance on how it should handle AWS S3 storage operations.

When to use this skill:

  • Uploading rendered videos to S3 storage
  • Configuring S3Client with credentials and region
  • Implementing PutObjectCommand operations
  • Handling S3 upload errors and retries
  • Managing object keys and content-type headers
  • Cleaning up temporary files after upload

Instructions

  • AWS SDK v3: Use @aws-sdk/client-s3 for S3 operations with proper credential configuration
  • Credential Management: Load credentials from Docker secrets at /run/secrets/ in production
  • Error Handling: Wrap S3 operations in try-catch; implement retry logic for transient failures
  • Presigned URLs: Consider using presigned URLs for temporary access instead of public objects
  • Object Keys: Use consistent naming pattern for video files (e.g., videos/YYYY-MM-recap-{uuid}.mp4)
  • Content Type: Set correct content-type header (video/mp4) when uploading
  • Cleanup: Delete temporary local files after successful S3 upload

Examples:

// Good: Proper error handling, retry logic, cleanup
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import fs from 'fs';

const s3Client = new S3Client({
  region: process.env.AWS_REGION,
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
  },
});

async function uploadToS3(filePath: string, key: string): Promise<string> {
  const fileStream = fs.createReadStream(filePath);

  try {
    await s3Client.send(new PutObjectCommand({
      Bucket: process.env.S3_BUCKET_NAME!,
      Key: key,
      Body: fileStream,
      ContentType: 'video/mp4',
    }));

    const url = `https://${process.env.S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`;
    return url;
  } catch (error) {
    throw new Error(`S3 upload failed: ${error.message}`);
  } finally {
    fileStream.close();
    fs.unlinkSync(filePath); // Cleanup temp file
  }
}

// Bad: No error handling, no cleanup, missing config
async function upload(file) {
  const s3 = new S3Client({});
  await s3.send(new PutObjectCommand({ Bucket: 'bucket', Key: 'video.mp4', Body: file }));
}