Amazon S3中是否有重命名文件和文件夹的功能?欢迎提出相关建议。
当前回答
没有办法通过GUI重命名文件夹,最快的(如果你喜欢GUI的话也是最简单的)方法是执行一个普通的旧拷贝。要实现这一点:使用GUI在S3上创建新文件夹,找到旧文件夹,选择全部,标记“复制”,然后导航到新文件夹并选择“粘贴”。完成后,删除旧文件夹。
这个简单的方法非常快,因为它是从S3复制到自身(不需要重新上传或类似的事情),而且它还维护所复制对象的权限和元数据。
其他回答
要重命名文件夹(从技术上讲,文件夹是一组对象,以公共前缀作为键),您可以使用aws CLI move命令带——recursive选项。
aws s3 mv s3://bucket/old_folder s3://bucket/new_folder --recursive
有一种软件可以使用s3桶执行不同类型的操作。
软件名称:S3浏览器
S3浏览器是Amazon S3和Amazon CloudFront的免费Windows客户端。Amazon S3提供了一个简单的web服务接口,可用于在任何时间、从web上的任何地方存储和检索任意数量的数据。Amazon CloudFront是一个内容分发网络(CDN)。它可用于使用边缘位置的全球网络交付您的文件。
如果只有一次,那么你可以使用命令行执行这些操作:
(1)重命名同一桶内的文件夹:
s3cmd --access_key={access_key} --secret_key={secret_key} mv s3://bucket/folder1/* s3://bucket/folder2/
(2)重命名Bucket:
s3cmd --access_key={access_key} --secret_key={secret_key} mv s3://bucket1/folder/* s3://bucket2/folder/
在那里,
{access_key} = s3客户端的有效访问密钥
{secret_key} = s3客户端的有效密匙
它工作得很好,没有任何问题。
谢谢
s3中的文件夹结构有很多“问题”,似乎存储是扁平的。
我有一个Django项目,我需要重命名文件夹,但仍然保持目录结构不变,这意味着空文件夹也需要复制并存储在重命名的目录中。
aws cli很棒,但cp或sync或mv都没有将空文件夹(即以'/'结尾的文件)复制到新的文件夹位置,所以我使用boto3和aws cli的混合来完成任务。
或多或少我找到重命名目录中的所有文件夹,然后使用boto3将它们放在新位置,然后我用aws cli对数据进行cp,最后将其删除。
import threading
import os
from django.conf import settings
from django.contrib import messages
from django.core.files.storage import default_storage
from django.shortcuts import redirect
from django.urls import reverse
def rename_folder(request, client_url):
"""
:param request:
:param client_url:
:return:
"""
current_property = request.session.get('property')
if request.POST:
# name the change
new_name = request.POST['name']
# old full path with www.[].com?
old_path = request.POST['old_path']
# remove the query string
old_path = ''.join(old_path.split('?')[0])
# remove the .com prefix item so we have the path in the storage
old_path = ''.join(old_path.split('.com/')[-1])
# remove empty values, this will happen at end due to these being folders
old_path_list = [x for x in old_path.split('/') if x != '']
# remove the last folder element with split()
base_path = '/'.join(old_path_list[:-1])
# # now build the new path
new_path = base_path + f'/{new_name}/'
# remove empty variables
# print(old_path_list[:-1], old_path.split('/'), old_path, base_path, new_path)
endpoint = settings.AWS_S3_ENDPOINT_URL
# # recursively add the files
copy_command = f"aws s3 --endpoint={endpoint} cp s3://{old_path} s3://{new_path} --recursive"
remove_command = f"aws s3 --endpoint={endpoint} rm s3://{old_path} --recursive"
# get_creds() is nothing special it simply returns the elements needed via boto3
client, resource, bucket, resource_bucket = get_creds()
path_viewing = f'{"/".join(old_path.split("/")[1:])}'
directory_content = default_storage.listdir(path_viewing)
# loop over folders and add them by default, aws cli does not copy empty ones
# so this is used to accommodate
folders, files = directory_content
for folder in folders:
new_key = new_path+folder+'/'
# we must remove bucket name for this to work
new_key = new_key.split(f"{bucket}/")[-1]
# push this to new thread
threading.Thread(target=put_object, args=(client, bucket, new_key,)).start()
print(f'{new_key} added')
# # run command, which will copy all data
os.system(copy_command)
print('Copy Done...')
os.system(remove_command)
print('Remove Done...')
# print(bucket)
print(f'Folder renamed.')
messages.success(request, f'Folder Renamed to: {new_name}')
return redirect(request.META.get('HTTP_REFERER', f"{reverse('home', args=[client_url])}"))
下面是你如何在。net中使用S3 .NET SDK:
var client = new Amazon.S3.AmazonS3Client(_credentials, _config);
client.CopyObject(oldBucketName, oldfilepath, newBucketName, newFilePath);
client.DeleteObject(oldBucketName, oldfilepath);
附注:尽可能使用“异步”版本的客户端方法,尽管我没有这样做是为了可读性
重命名所有*.csv. csv文件。将<<桶>>/landing dir中的Err文件转换为带有s3cmd的*.csv文件
export aws_profile='foo-bar-aws-profile'
while read -r f ; do tgt_fle=$(echo $f|perl -ne 's/^(.*).csv.err/$1.csv/g;print'); \
echo s3cmd -c ~/.aws/s3cmd/$aws_profile.s3cfg mv $f $tgt_fle; \
done < <(s3cmd -r -c ~/.aws/s3cmd/$aws_profile.s3cfg ls --acl-public --guess-mime-type \
s3://$bucket | grep -i landing | grep csv.err | cut -d" " -f5)
推荐文章
- 如何查看所有地区所有正在运行的Amazon EC2实例?
- 如何从命令行使用多个AWS帐户?
- 如何搜索亚马逊s3桶?
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 我如何使用通配符“cp”一组文件与AWS CLI
- 我如何获得亚马逊的AWS_ACCESS_KEY_ID ?
- 如何使所有对象在AWS S3桶公共默认?
- 为什么我应该使用亚马逊Kinesis而不是SNS-SQS?
- 如何重命名AWS S3 Bucket
- AWS ECS中的任务和服务之间有什么区别?
- 亚马逊SimpleDB vs亚马逊DynamoDB
- 亚马逊ECS和亚马逊EC2有什么区别?
- 我如何知道我在S3桶中存储了多少对象?
- S3 Bucket操作不应用于任何资源
- 将AWS凭证传递给Docker容器的最佳方法是什么?