Spaces:
Running
on
Zero
Running
on
Zero
class S3Uploader { | |
constructor(accessKey, secretKey) { | |
this.accessKey = accessKey; | |
this.secretKey = secretKey; | |
} | |
uploadFile(file) { | |
var s3 = getS3Service(this.accessKey, this.secretKey); | |
var fileBlob = file.getBlob(); | |
var fileContent = fileBlob.getBytes(); | |
var fileMetadata = { | |
'Content-Type': file.getMimeType(), | |
'Content-Length': fileBlob.getBytes().length | |
}; | |
s3.putObject('my-bucket', file.getName(), fileContent, fileMetadata); | |
} | |
getS3Service(accessKey, secretKey) { | |
var service = OAuth2.createService('s3') | |
.setAuthorizationBaseUrl('https://s3.amazonaws.com/') | |
.setTokenUrl('https://s3.amazonaws.com/') | |
.setClientId(accessKey) | |
.setClientSecret(secretKey) | |
.setCallbackFunction('authCallback') | |
.setPropertyStore(PropertiesService.getUserProperties()); | |
return service; | |
} | |
authCallback(request) { | |
var service = getService(); | |
var authorized = service.handleCallback(request); | |
if (authorized) { | |
return HtmlService.createHtmlOutput('Authorized'); | |
} else { | |
return HtmlService.createHtmlOutput('Access denied'); | |
} | |
} | |
} |