MCPcopy
hub / github.com/simstudioai/sim / uploadToS3

Function uploadToS3

apps/sim/lib/uploads/providers/s3/client.ts:77–133  ·  view source on GitHub ↗
(
  file: Buffer,
  fileName: string,
  contentType: string,
  configOrSize?: S3Config | number,
  size?: number,
  skipTimestampPrefix?: boolean,
  metadata?: Record<string, string>
)

Source from the content-addressed store, hash-verified

75 * @returns Object with file information
76 */
77export async function uploadToS3(
78 file: Buffer,
79 fileName: string,
80 contentType: string,
81 configOrSize?: S3Config | number,
82 size?: number,
83 skipTimestampPrefix?: boolean,
84 metadata?: Record<string, string>
85): Promise<FileInfo> {
86 let config: S3Config
87 let fileSize: number
88 let shouldSkipTimestamp: boolean
89
90 if (typeof configOrSize === 'object') {
91 config = configOrSize
92 fileSize = size ?? file.length
93 shouldSkipTimestamp = skipTimestampPrefix ?? false
94 } else {
95 config = { bucket: S3_CONFIG.bucket, region: S3_CONFIG.region }
96 fileSize = configOrSize ?? file.length
97 shouldSkipTimestamp = skipTimestampPrefix ?? false
98 }
99
100 const safeFileName = sanitizeFileName(fileName)
101 const uniqueKey = shouldSkipTimestamp ? fileName : `${Date.now()}-${safeFileName}`
102
103 const s3Client = getS3Client()
104
105 const s3Metadata: Record<string, string> = {
106 originalName: sanitizeFilenameForMetadata(fileName),
107 uploadedAt: new Date().toISOString(),
108 }
109
110 if (metadata) {
111 Object.assign(s3Metadata, sanitizeStorageMetadata(metadata, 2000))
112 }
113
114 await s3Client.send(
115 new PutObjectCommand({
116 Bucket: config.bucket,
117 Key: uniqueKey,
118 Body: file,
119 ContentType: contentType,
120 Metadata: s3Metadata,
121 })
122 )
123
124 const servePath = `/api/files/serve/${encodeURIComponent(uniqueKey)}`
125
126 return {
127 path: servePath,
128 key: uniqueKey,
129 name: fileName,
130 size: fileSize,
131 type: contentType,
132 }
133}
134

Callers 2

client.test.tsFile · 0.90
uploadFileFunction · 0.85

Calls 5

sanitizeFileNameFunction · 0.90
sanitizeStorageMetadataFunction · 0.90
getS3ClientFunction · 0.85
sendMethod · 0.80

Tested by

no test coverage detected