I want to Upload into GCS using Typescript.
I am using Service Account Json for credentials.
When I tested it it says Uploaded Successfully. But, when I checked the server there was no
file uploaded. Any Idea what this might cause?
import { Storage } from '@google-cloud/storage'
import { SSM } from 'aws-sdk'
import fs from 'fs'
import os from 'os'
export async function uploadFileToGCS(filename: string, bucketName: string): Promise<any> {
// GCSサービスアカウントの認証情報を指定します。
const serviceAccountCredentials = await getParameter()
const credentials = JSON.parse(serviceAccountCredentials)[0]
const projectId = 'my-project-id'
const storage = new Storage({
projectId: projectId,
credentials: credentials,
})
const filePath = os.tmpdir()
// Create CSV
fs.writeFileSync(`${filePath}/${filename}`, '', { encoding: 'utf-8' }) // Write the file to the writable directory
console.log(`CSV file "${filename}" has been created.`)
try {
const bucket = storage.bucket(bucketName)
await bucket.upload(`${filePath}/${filename}`, {
destination: filename,
})
console.log(`File ${filename} uploaded to ${bucketName}.`)
return Promise.resolve({
statusCode: 200,
body: 'Successfully Uploaded',
})
} catch (err) {
console.error('Error uploading file:', err)
return Promise.resolve({
statusCode: 500,
body: 'Failed with Uploading',
})
}
}
I am expecting the file to be uploaded into the server.