How to rename files and folder in Amazon S3?

后端 未结 19 2399
暖寄归人
暖寄归人 2020-11-28 03:01

Is there any function to rename files and folders in Amazon S3? Any related suggestions are also welcome.

19条回答
  •  盖世英雄少女心
    2020-11-28 03:36

    Below is the code example to rename file on s3. My file was part-000* because of spark o/p file, then i copy it to another file name on same location and delete the part-000*:

    import boto3
    client = boto3.client('s3')
    response = client.list_objects(
    Bucket='lsph',
    MaxKeys=10,
    Prefix='03curated/DIM_DEMOGRAPHIC/',
    Delimiter='/'
    )
    name = response["Contents"][0]["Key"]
    copy_source = {'Bucket': 'lsph', 'Key': name}
    client.copy_object(Bucket='lsph', CopySource=copy_source, 
    Key='03curated/DIM_DEMOGRAPHIC/'+'DIM_DEMOGRAPHIC.json')
    client.delete_object(Bucket='lsph', Key=name)
    

提交回复
热议问题