57 lines
2.9 KiB
TypeScript
57 lines
2.9 KiB
TypeScript
|
|
import { BlobServiceClient, BlockBlobClient} from '@azure/storage-blob';
|
|
|
|
export const uploadImagesToAzure = async (files: File[], task_idx: string): Promise<string[]> => {
|
|
// const sasUrl = process.env.AZURE_STORAGE_SAS_URL;
|
|
// // Default to 'castad-data' if not set, or read from env
|
|
// const rootPath = process.env.AZURE_UPLOAD_ROOT || "castad-data";
|
|
// const containerName = "castad-uploads"; // Ensure this matches your SAS token permissions or container
|
|
|
|
// if (!sasUrl) {
|
|
// console.warn("AZURE_STORAGE_SAS_URL is missing in .env. Returning mock URLs.");
|
|
// return files.map((_, i) => `https://mock-azure.com/${rootPath}/${task_idx}/img/${i}.jpg`);
|
|
// }
|
|
const AZURE_SAS_TOKEN="sv=2024-11-04&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2027-03-12T17:32:56Z&st=2025-12-08T09:17:56Z&spr=https,http&sig=J0uF91mTBpSUVSk0O%2FG2IXmebCDRMYp1%2BNOtBVpcOKE%3D"
|
|
const AZURE_SERVICE_PATH="https://ado2mediastoragepublic.blob.core.windows.net/"
|
|
// // const rootPath = "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/dev-user-idx"
|
|
// // const blobPath =
|
|
try {
|
|
|
|
// const sas_url = `https://ado2mediastoragepublic.blob.core.windows.net/?sv=2024-11-04&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2027-03-12T17:32:56Z&st=2025-12-08T09:17:56Z&spr=https,http&sig=J0uF91mTBpSUVSk0O%2FG2IXmebCDRMYp1%2BNOtBVpcOKE%3D`;
|
|
// const blobServiceClient = new BlobServiceClient(AZURE_SERVICE_PATH,);
|
|
// const blobContainerClient = blobServiceClient.getContainerClient("ado2-media-public-access")
|
|
// const blockBlobClient = new BlockBlobClient
|
|
|
|
const uploadPromises = files.map(async (file, index) => {
|
|
const extension = file.name.split('.').pop() || 'jpg';
|
|
// Path format: ROOT/task_id/img/uploadNumber.extension
|
|
|
|
const blobName = `${AZURE_SERVICE_PATH}ado2-media-public-access/ado2-media-original/dev-user-idx/dev-task-idx/${task_idx}-img-${index}.${extension}`;
|
|
const sasUrl = `${blobName}?${AZURE_SAS_TOKEN}`
|
|
// // console.log(`\nBlobUrl = ${sasUrl}\n`);
|
|
// const blockBlobClient = blobContainerClient.getBlockBlobClient(blobName);
|
|
|
|
// await blockBlobClient.uploadData(file, {
|
|
// blobHTTPHeaders: { blobContentType: file.type }
|
|
// });
|
|
// console.log(blockBlobClient.url)
|
|
// return blockBlobClient.url;
|
|
// var data = new FormData()
|
|
// data.append('file', file)
|
|
fetch(sasUrl, {
|
|
headers: {"Content-Type": file.type,
|
|
"x-ms-blob-type" : "BlockBlob"},
|
|
method: 'PUT',
|
|
body: file // FormData 객체를 직접 body에 전달하면 Content-Type이 자동으로 설정됨 (multipart/form-data)
|
|
})
|
|
console.log(blobName)
|
|
return blobName
|
|
});
|
|
return await Promise.all(uploadPromises);
|
|
}
|
|
catch (error) {
|
|
console.error("Azure Upload Error:", error);
|
|
throw new Error("Failed to upload images to Azure.");
|
|
}
|
|
};
|