如何使用特定树将文件存储在Bucket s3上,然后将其压缩

时间:2017-09-11 15:28:41

标签: python json amazon-s3 smartsheet-api

我正在研究这个问题,但amazon aws s3不是我最喜欢的部分,所以我需要一些帮助:

import smartsheet
import time
import os
import json
import shutil
import zipfile
import boto3

#Initialisation Variables et Environment

token = api_token
ss = smartsheet.Smartsheet(token)
ssh = ss.Home.list_all_contents()
backupdir = "d:\\Backup"
if not os.path.exists(backupdir):
    os.mkdir(backupdir)
zipname = "SmartsheetBackup - " + time.strftime('%Y-%m-%d-%H-%M')
odir = "d:\\Backup\\SmartsheetBackup - " + time.strftime('%Y-%m-%d-%H-%M')
lsthome = json.loads(ssh.to_json())
sss = ss.Sheets.list_sheets(include_all=True) 
lstsheetsall = json.loads(sss.to_json())
rapport = open(odir + " Report.txt", "w+")
rapportname = zipname + " Report.txt"
lstsheets = []
workspaces = ss.Workspaces.list_workspaces(include_all=True)
lstworkspaces = json.loads(workspaces.to_json())
s3 = boto3.client('s3')
bucket = s3.Bucket(bucketname)

#Fin Initialisation

所以实际上我们得到了这个设置:

  • api_token是用于备份数据的令牌智能表
  • 将随存储桶更改的backupdir
  • zipname创建zip的最终名称
  • ss,ssh,lsthom,lstsheetsall,lstsheets,workspace,lstworkspaces特定于我的smartsheet-api var
  • 和存储桶名称
  • 的存储桶s3 var
#Création Dossier de backup, rapport de backup

os.mkdir(odir)
rapport.write("---------- Starting Backup ----------\n\n")
rapport.write(time.strftime('%Y-%m-%d-%H-%M\n\n'))

这里我创建了一个报告文件,我将把时间戳等等......

#Backup des dossiers et sheets Inférieurs ou égales à F+2

print("Tool About to backup folder and sheets equals and less than f+2")
rapport.write("Start Backup Folders <= F+2\n\n")
for folder in lsthome['folders']:
    rapport.write("Create Folder : " + folder['name'] + "\n")
    os.mkdir(odir + "\\" + folder['name'])
    os.chdir(odir + "\\" + folder['name'])
    cdir = os.getcwd()
    for sheets in folder['sheets']:
        rapport.write("Download : " + sheets['name'] + "\n")
        ss.Sheets.get_sheet_as_excel(sheets['id'], os.getcwd())
        lstsheets.append(sheets['id'])
    if folder['folders'] == '[]':
        break
    else:
        for sfolder in folder['folders']:
            rapport.write("Create Folder : " + sfolder['name'] + "\n")
            os.mkdir(sfolder['name'])
            os.chdir(sfolder['name'])
            for ssheets in sfolder['sheets']:
                rapport.write("Download : " + ssheets['name'] + "\n")
                ss.Sheets.get_sheet_as_excel(ssheets['id'], os.getcwd())
                lstsheets.append(ssheets['id'])
rapport.write("Folder and sheets f+2 Backup OK \n\n")
print("Folders And Sheets Backup OK !")

#Fin De Backup des Dossiers

然后我使用正确的树备份文件夹和文件夹+ 2 max。 (使用s3它应该更快)

#Initialisation du Backup des fichiers root

print("Tool About to backup Root Sheets")
rapport.write("Download Root Sheets \n")
for bsheets in lsthome['sheets']:
    os.chdir(odir)
    ss.Sheets.get_sheet_as_excel(bsheets['id'], os.getcwd())
    rapport.write("Download Rsheets : " + bsheets['name'] + "\n")
    lstsheets.append(bsheets['id'])
rapport.write("Root Sheets Backup OK \n\n")
print("Root Sheets Bakcup OK !")

#Fin de backup des fichier root

有根文件的备份

#Backup des sheets restants dans SheetsSup

os.chdir(odir)
print("Tool About to backup file in more than f+2")
os.mkdir(odir + "\\SheetsSup")
os.chdir(odir + "\\SheetsSup")
rapport.write("\n\nBackup sheets over f+2\n\n")
if not(str(lstsheetsall['totalCount']) == str(len(lstsheets))):
    for sheetsall in lstsheetsall['data']:
        if not(sheetsall['id'] in lstsheets):
            rapport.write("Download : " + sheetsall['name'] + "\n")
            ss.Sheets.get_sheet_as_excel(sheetsall['id'], os.getcwd())
    os.chdir(odir)
rapport.write("Sheets Backup OK \n")
print("Sheets Backup Sup OK !")

#Fin backup Sheets over F+2

以下是其余文件(在文件夹+ 2上)

#Initialisation Backup Attachments

rapport.write("\n\nStart Backup Attachments\n\n")
print("Tool About to backup Attachments")
os.mkdir(odir + "\\Attachments")
os.chdir(odir + "\\Attachments")
for sheetsall in lstsheetsall['data']:
    DLA = ss.Attachments.list_all_attachments(sheetsall['id'], include_all=True)
    action = DLA.data
    for col in action:
        rapport.write("Download : " + col.name + "\n")
        attachurl = ss.Attachments.get_attachment(sheetsall['id'],col.id)
        ss.Attachments.download_attachment(attachurl, os.getcwd())
print("Download Attachments OK !")
rapport.write("Download Attach OK")

#Fin de backup attachments

文件和文件夹智能表中的附件

#Initialisation Zip Dossier Backup + Fichier rapport

os.chdir(backupdir)
print("Zipping Backup")
rapport.write("Zipping Archive")
shutil.make_archive(zipname, 'zip', odir)
z = zipfile.ZipFile(zipname + ".zip", 'a')
rapport.write("\n\nRapport end at : " + time.strftime('%Y-%m-%d-%H-%M') + "\n")
rapport.write("----------- Ending Backup -----------")
rapport.close()
z.write(rapportname)
z.close()
print("Zipping Complete !")

#Fin de Zipping Dossier

#Néttoyage fichier et dossier

shutil.rmtree(odir)
os.remove(rapportname)

#Fin de Programme

压缩文件夹根目录然后删除文件以保留zip。

问题

现在问题是如何制作相同的程序,但将我的d:\文件夹的本地存储替换为s3存储。

这个Python程序应该运行一个lambda,它不时安排它(例如:每天或2天)。所以本地PC之间没有过渡。

1 个答案:

答案 0 :(得分:2)

您需要下载到本地存储,然后上传到S3。

Boto(https://github.com/boto/boto3)是S3的Python库。