SHARE
TWEET

Untitled

a guest Apr 18th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. import boto3
  3.  
  4.  
  5. def lambda_handler(event, context):
  6.     res = ''
  7.     s3 = boto3.client('s3')
  8.  
  9.     if(len(event) == 0):
  10.         with open('index.html','r') as file:
  11.             return file.read()
  12.     elif(event['command'] == 'list' ):
  13.         s3 = boto3.resource('s3')
  14.         bucketNames = []
  15.         for bucket in s3.buckets.all():
  16.             bucketNames.append(bucket.name)
  17.         return ', '.join(bucketNames)
  18.     elif event['command'] == 'create':
  19.         s3.create_bucket(Bucket=event['bucketName'],CreateBucketConfiguration={'LocationConstraint' : 'eu-west-3'})
  20.     elif event['command'] == 'delete':
  21.         s3.delete_bucket(Bucket=event['bucketName'])
  22.     elif event['command'] == 'dir_list':
  23.         my_bucket = boto3.resource('s3').Bucket(event['bucketName'])
  24.         objects = []
  25.         for el in my_bucket.objects.all():
  26.             objects.append(el.key)
  27.         return objects
  28.     elif event['command'] == 'deleteFile':
  29.         #s3 = boto3.resource('s3')
  30.         #s3.delete_object(Bucket=event['bucketName'],Key=event['fileName'])
  31.         s3client = boto3.client('s3')
  32.         s3client.delete_object(Bucket=event['bucketName'],Key=event['filename'])
  33.     elif event['command'] == 'upload':
  34.     #    s3.meta.client.upload_file(event['path'], event['bucketName'], event['fileName'] )
  35.         s3 = boto3.resource('s3')
  36.         s3.Bucket(event['bucketName']).put_object(Key=event['fileName'], Body=event['fileData'])
  37.     elif event['command'] == 'download':
  38.         #s3.meta.client.upload_file( event['bucketName'], event['fileName'], event['path'] )
  39.         s3.download_file(event['bucketName'], event['fileName'],event['fileName'])
  40.     elif 1==1:
  41.         pass
  42.        
  43.     return res
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top