0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【NestJS】GCSからSigned URLをクライアントサイドに返却するアレコレ

Posted at

概要

Cloud Storageに保存している画像のSigned URLをクライアントサイドに返却するまでのプロセスをまとめる

やり方

@Injectable()
export class GcpClientService {
    private readonly storage: Storage
    private readonly auth: GoogleAuth

    constructor(
        private configService: ConfigService
    ) {
        // https://www.npmjs.com/package/@google-cloud/storage
        this.storage = new Storage({
            projectId: this.configService.get('gcp.projectId'),
            authClient: this.auth,
        })
    }

    async downloadFiles(id: string, file_tree_id: number) {
        const fileMap = await this.backupFileMapRepository.findOne({
            where: {id: file_tree_id, is_latest: true},
        })

        if (!fileMap) {
            throw new NotFoundException('ファイルツリーが見つかりませんでした')
        }

        const storage = this.gcpClientService.getStorage()

        return await storage
            .bucket(contractor.gcp_storage_bucket_name)
            .file(`${id}/blob/${fileMap.file_hash}`)
            .getSignedUrl({
                action: 'read',
                expires: Date.now() + 15 * 60 * 1000,
                queryParams: {
                    'response-content-disposition': `attachment; filename=${path.basename(
                        fileMap.path
                    )}`,
                },
            })
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?