python tar.gz一个目录并将其推送到Glacier。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python tar.gz一个目录并将其推送到Glacier。相关的知识,希望对你有一定的参考价值。

from boto import glacier
from datetime import datetime
from os.path import isdir
import argparse
import tarfile

try:
	# Python 2.7
	from collections import OrderedDict
except ImportError:
	# Python 2.6 (after "pip install ordereddict")
	from ordereddict import OrderedDict


params = OrderedDict([
	("access_key", {}),
	("secret_key", {}),
	("target", {"help": "The file or directory you want to tar.gz up and push to Glacier."}),
	("vault", {"help": "The name of the vault to push to. Will be created if it doesn't exist."}),
	("--region", {"default": "eu-west-1"}),
	("--tar_gz", {"action": "store_true", "default": False})
])

def get_glacier_connection(region, access_key, secret_key):
	return glacier.connect_to_region(
		region,
		aws_access_key_id=access_key,
		aws_secret_access_key=secret_key)

def create_tar_gz(name, target):
	try:
		tar = tarfile.open(name, "w:gz")
		tar.add(target)
	finally:
		tar.close()

def get_or_create_vault(name, region):
	print "Listing vaults..."
	vault = None
	for v in region.list_vaults():
		if v.name == name:
			print "Existing vault %s found!" % name
			vault = v
			break

	if vault is None:
		print "No existing vault was found. Creating one..."
		vault = region.create_vault(name)
	return vault

def main():
	a = argparse.ArgumentParser()
	for key, value in params.items():
		a.add_argument(key, **value)

	args = a.parse_args()

	print "Connecting to %s region..." % args.region
	region = get_glacier_connection(args.region, args.access_key, args.secret_key)

	vault = get_or_create_vault(args.vault, region)

	print "Uploading archive..."
	archive_id = vault.upload_archive(
		args.target,
		description=str(datetime.now()))

	print "Succesfully uploaded archive ID: %s" % archive_id

	print "DONE"

if __name__ == "__main__":
	main()

以上是关于python tar.gz一个目录并将其推送到Glacier。的主要内容,如果未能解决你的问题,请参考以下文章

构建 docker-compose 镜像并将其推送到容器注册表中

遍历一个数组,将每个项目添加到一个对象并将其推送到 Javascript 中的数组

sh 复制Boilerplate并将其推送到新的Repo

我可以使用 AWS Glue 将 S3 上的 json 数据转换为列格式并将其推送到 Redshift 吗?

如何创建现有项目文件夹,SVN存储库并将其推送到远程仓库

Nexus OSS - 我如何添加分支名称并在rpm包名称下构建no并将其推送到nexus oss?