2016-10-26 228 views
3

我是一名JavaScript/Angular 2開發人員,他正在使用Bitbucket管道,Python和Boto參與s3集成的部署。我昨天才被介紹給這三種技術!Boto:將多個文件上傳到s3

我的webpack版本生成一個文件夾dist,它包含我想上傳到s3的所有文件。我希望這些文件出現在s3存儲桶的根目錄中。

我在bitbucket-pipelines.yaml如下:

image: node:5.6.0 

pipelines: 
    default: 
    - step: 
     script: 
      # other stuff.., 
      - python s3_upload.py io-master.mycompany.co.uk dist io-dist 

Here是整個Python s3_upload.py

正如你所看到的,腳本使用put_object

client.put_object(
    Body=open(artefact, 'rb'), 
    Bucket=bucket, 
    Key=bucket_key 
) 

我想要做的就是將dist文件夾的內容上傳到s3。爲了能夠做到這一點,我需要學習Python嗎?還是Boto中有一種方法可以做到這一點?

回答

0
def sync_to_s3(target_dir, aws_region=AWS_REGION, bucket_name=BUCKET_NAME): 
    if not os.path.isdir(target_dir): 
     raise ValueError('target_dir %r not found.' % target_dir) 

    s3 = boto3.resource('s3', region_name=aws_region) 
    try: 
     s3.create_bucket(Bucket=bucket_name, 
         CreateBucketConfiguration={'LocationConstraint': aws_region}) 
    except ClientError: 
     pass 

    for filename in os.listdir(target_dir): 
     logger.warn('Uploading %s to Amazon S3 bucket %s' % (filename, bucket_name)) 
     s3.Object(bucket_name, filename).put(Body=open(os.path.join(target_dir, filename), 'rb')) 

     logger.info('File uploaded to https://s3.%s.amazonaws.com/%s/%s' % (
      aws_region, bucket_name, filename)) 

你可以通過一個文件夾作爲參數,遍歷文件