File size: 1,157 Bytes
3d979b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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');
    }
  }
}