private async uploadFiles()

in src/tasks/S3Upload/TaskOperations.ts [129:204]


    private async uploadFiles() {
        let msgTarget: string
        if (this.taskParameters.targetFolder) {
            msgTarget = this.taskParameters.targetFolder
        } else {
            msgTarget = '/'
        }
        console.log(
            tl.loc('UploadingFiles', this.taskParameters.sourceFolder, msgTarget, this.taskParameters.bucketName)
        )

        const matchedFiles = this.findMatchingFiles(this.taskParameters)

        if (matchedFiles.length === 0) {
            tl.warning(
                tl.loc('NoMatchingFilesFound', this.taskParameters.sourceFolder, this.taskParameters.globExpressions)
            )
        }

        for (const matchedFile of matchedFiles) {
            let relativePath = matchedFile.substring(this.taskParameters.sourceFolder.length)
            if (relativePath.startsWith(path.sep)) {
                relativePath = relativePath.slice(1)
            }
            let targetPath = relativePath

            if (this.taskParameters.flattenFolders) {
                const flatFileName = path.basename(matchedFile)
                if (this.taskParameters.targetFolder) {
                    targetPath = path.join(this.taskParameters.targetFolder, flatFileName)
                } else {
                    targetPath = flatFileName
                }
            } else {
                if (this.taskParameters.targetFolder) {
                    targetPath = path.join(this.taskParameters.targetFolder, relativePath)
                } else {
                    targetPath = relativePath
                }
            }

            targetPath = targetPath.replace(/\\/g, '/')
            const stats = fs.lstatSync(matchedFile)
            if (!stats.isDirectory()) {
                const fileBuffer = fs.createReadStream(matchedFile)
                try {
                    let contentType: string | false
                    if (this.taskParameters.contentType) {
                        contentType = this.taskParameters.contentType
                    } else {
                        contentType = lookup(path.extname(matchedFile))
                        if (!contentType) {
                            contentType = 'application/octet-stream'
                        }
                    }
                    console.log(tl.loc('UploadingFile', matchedFile, contentType))

                    const request: S3.PutObjectRequest = {
                        Bucket: this.taskParameters.bucketName,
                        Key: targetPath,
                        Body: fileBuffer,
                        ContentType: contentType,
                        StorageClass: this.taskParameters.storageClass
                    }

                    this.buildS3Request(request, matchedFile)

                    await this.s3Client.upload(request).promise()
                    console.log(tl.loc('FileUploadCompleted', matchedFile, targetPath))
                } catch (err) {
                    console.error(tl.loc('FileUploadFailed'), err)
                    throw err
                }
            }
        }
    }