python 遍歷oss 實現批次下載
oss儲存有很多批次上傳的文 = = 然後現在是由於程式主動刪除了oss的檔案,阿里是不給恢復的,所以想要把檔案備份到本地 。
阿里的oss是key-values形式的儲存,沒有資料夾的概念,也是就是說test/007.jpg實際上是兩個檔案 一個叫做test/的空檔案和一個叫做test/007.jpg 的檔案
看了ossfs osscmd 都可以實現批次下載,然後ossfs掛載上去對其操作都是在oss端的操作,後者 還沒用。
故此,自己寫了個很爛的程式碼。先暫時滿足下需求。囧 ,不行的話還是採用osscmd多執行緒支援斷電續傳吧 囧。沒辦法 渣呀。。。。。。。
直接貼了 。。。還有 模組記得安裝 pip install oss2
#coding:utf-8
from __future__ import print_function
import os,sys
import oss2
from itertools import islice
import urllib
import requests
import re
auth = oss2.Auth('您的AccessKeyId', '您的AccessKeySecret')
bucket = oss2.Bucket(auth, '地區', 'bucket桶名')
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExits = os.path.exists(path)
if not isExits:
print(path + "建立成功")
os.makedirs(path)
return True
else:
print(path+"目錄存在")
return False
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
for b in islice(oss2.ObjectIterator(bucket),sys.maxsize):
c = b.key
if c[-1:] == '/':
mkdir(c)
if c[-1:] != '/':
oss2.resumable_download(bucket, c, c,store=oss2.ResumableDownloadStore(root='/tmp'),multiget_threshold=20*1024*1024,part_size=10*1024*1024,num_threads=3,progress_callback=percentage)
print(c+"下載完成")
然後 期間建立目錄看的是別人的文件,初學python 只能按別人的程式碼來 TOT。總感覺這樣做太低效了,有其他好點的辦法麼 ,這樣拼拼湊湊的程式碼 不忍直視 囧o(╯□╰)o 。 Python2.6 Python3.4都可以跑。
來自
升級版 : 增量下載 o(╯□╰)o
#coding:utf-8
from __future__ import print_function
import os,sys
import oss2
from itertools import islice
import urllib
import requests
import re
import time
auth = oss2.Auth('同上', '同上')
bucket = oss2.Bucket(auth, '同上', '同上')
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExits = os.path.exists(path)
if not isExits:
print(path + "建立成功")
os.makedirs(path)
return True
else:
print(path+"目錄已經存在")
return False
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
filename = time.strftime("%Y-%m-%d-%H%M%S", time.localtime())
for b in islice(oss2.ObjectIterator(bucket),sys.maxsize):
c = b.key
if c[-1:] == '/':
mkdir(c)
if c[-1:] != '/':
remote_stream = bucket.get_object(c)
print(remote_stream.last_modified)
ticks = time.time()
# file = open('time.txt','w')
# file.write(str(ticks))
file = open('time.txt','r')
f = file.read()
k = float(f)
if remote_stream.last_modified > k:
oss2.resumable_download(bucket, c, c,store=oss2.ResumableDownloadStore(root='/tmp'),multiget_threshold=20*1024*1024,part_size=10*1024*1024,num_threads=3,progress_callback=percentage)
#filename = time.strftime("%Y-%m-%d-%H%M%S", time.localtime())
print(filename)
fileobject = open(filename,'a')
fileobject.write(str(c))
#fileobject.close()
#bucket.get_object_to_file(c,c)
file = open('time.txt','w')
file.write(str(ticks))
file.close()
fileobject.close()
缺點:遍歷 嗯 然後目錄沒有做提取 。然後下載地點就是本地 。之後再改改。哈哈
斷點下載= =
#coding:utf-8
from __future__ import print_function
import os,sys
import oss2
from itertools import islice
import urllib
import requests
import re
auth = oss2.Auth('XXX', 'XXX')
bucket = oss2.Bucket(auth, 'XXX', 'XXX')
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExits = os.path.exists(path)
if not isExits:
print(path + "建立成功")
os.makedirs(path)
return True
else:
print(path+"目錄已經存在")
return False
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
for b in islice(oss2.ObjectIterator(bucket),sys.maxsize):
c = b.key
if c[-1:] == '/':
mkdir('/ossbackup/ossback/'+c)
else:
cname = str(c)
sumnumber = 0
i = 0
for line2 in open('downname.txt','r'):
line1 = str(line2)
cname2 = str(cname+'\n')
if cname2 != line1:
i = i + 0
sumnumber = i
else:
i = i + 1
sumnumber = i
# print(sumnumber)
if sumnumber == 0 :
downfilename = open('downfilename.txt','w')
downfilename.write(str(cname))
print(cname+'is not download')
downfilename.close()
else :
print(cname+' is download')
downfilename = open('downfilename.txt','r')
lineend = downfilename.readline()
oss2.resumable_download(bucket,lineend,'/ossbackup/ossback/'+lineend,store=oss2.ResumableDownloadStore(root='/tmp'),multiget_threshold=20*1024*1024,part_size=10*1024*1024,num_threads=3,progress_callback=percentage)
print(lineend+"下載完成"+"\n")
file = open('downname.txt','a')
file.write(str(lineend)+"\n")
阿里的oss是key-values形式的儲存,沒有資料夾的概念,也是就是說test/007.jpg實際上是兩個檔案 一個叫做test/的空檔案和一個叫做test/007.jpg 的檔案
看了ossfs osscmd 都可以實現批次下載,然後ossfs掛載上去對其操作都是在oss端的操作,後者 還沒用。
故此,自己寫了個很爛的程式碼。先暫時滿足下需求。囧 ,不行的話還是採用osscmd多執行緒支援斷電續傳吧 囧。沒辦法 渣呀。。。。。。。
直接貼了 。。。還有 模組記得安裝 pip install oss2
#coding:utf-8
from __future__ import print_function
import os,sys
import oss2
from itertools import islice
import urllib
import requests
import re
auth = oss2.Auth('您的AccessKeyId', '您的AccessKeySecret')
bucket = oss2.Bucket(auth, '地區', 'bucket桶名')
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExits = os.path.exists(path)
if not isExits:
print(path + "建立成功")
os.makedirs(path)
return True
else:
print(path+"目錄存在")
return False
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
for b in islice(oss2.ObjectIterator(bucket),sys.maxsize):
c = b.key
if c[-1:] == '/':
mkdir(c)
if c[-1:] != '/':
oss2.resumable_download(bucket, c, c,store=oss2.ResumableDownloadStore(root='/tmp'),multiget_threshold=20*1024*1024,part_size=10*1024*1024,num_threads=3,progress_callback=percentage)
print(c+"下載完成")
來自
升級版 : 增量下載 o(╯□╰)o
#coding:utf-8
from __future__ import print_function
import os,sys
import oss2
from itertools import islice
import urllib
import requests
import re
import time
auth = oss2.Auth('同上', '同上')
bucket = oss2.Bucket(auth, '同上', '同上')
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExits = os.path.exists(path)
if not isExits:
print(path + "建立成功")
os.makedirs(path)
return True
else:
print(path+"目錄已經存在")
return False
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
filename = time.strftime("%Y-%m-%d-%H%M%S", time.localtime())
for b in islice(oss2.ObjectIterator(bucket),sys.maxsize):
c = b.key
if c[-1:] == '/':
mkdir(c)
if c[-1:] != '/':
remote_stream = bucket.get_object(c)
print(remote_stream.last_modified)
ticks = time.time()
# file = open('time.txt','w')
# file.write(str(ticks))
file = open('time.txt','r')
f = file.read()
k = float(f)
if remote_stream.last_modified > k:
oss2.resumable_download(bucket, c, c,store=oss2.ResumableDownloadStore(root='/tmp'),multiget_threshold=20*1024*1024,part_size=10*1024*1024,num_threads=3,progress_callback=percentage)
#filename = time.strftime("%Y-%m-%d-%H%M%S", time.localtime())
print(filename)
fileobject = open(filename,'a')
fileobject.write(str(c))
#fileobject.close()
#bucket.get_object_to_file(c,c)
file = open('time.txt','w')
file.write(str(ticks))
file.close()
fileobject.close()
缺點:遍歷 嗯 然後目錄沒有做提取 。然後下載地點就是本地 。之後再改改。哈哈
斷點下載= =
#coding:utf-8
from __future__ import print_function
import os,sys
import oss2
from itertools import islice
import urllib
import requests
import re
auth = oss2.Auth('XXX', 'XXX')
bucket = oss2.Bucket(auth, 'XXX', 'XXX')
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExits = os.path.exists(path)
if not isExits:
print(path + "建立成功")
os.makedirs(path)
return True
else:
print(path+"目錄已經存在")
return False
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
for b in islice(oss2.ObjectIterator(bucket),sys.maxsize):
c = b.key
if c[-1:] == '/':
mkdir('/ossbackup/ossback/'+c)
else:
cname = str(c)
sumnumber = 0
i = 0
for line2 in open('downname.txt','r'):
line1 = str(line2)
cname2 = str(cname+'\n')
if cname2 != line1:
i = i + 0
sumnumber = i
else:
i = i + 1
sumnumber = i
# print(sumnumber)
if sumnumber == 0 :
downfilename = open('downfilename.txt','w')
downfilename.write(str(cname))
print(cname+'is not download')
downfilename.close()
else :
print(cname+' is download')
downfilename = open('downfilename.txt','r')
lineend = downfilename.readline()
oss2.resumable_download(bucket,lineend,'/ossbackup/ossback/'+lineend,store=oss2.ResumableDownloadStore(root='/tmp'),multiget_threshold=20*1024*1024,part_size=10*1024*1024,num_threads=3,progress_callback=percentage)
print(lineend+"下載完成"+"\n")
file = open('downname.txt','a')
file.write(str(lineend)+"\n")
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30629069/viewspace-2129464/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python遍歷之批次更改檔名Python
- jsp下實現遍歷集合JS
- 記一次 OSS 大批次檔案下載的實現 → bat指令碼不好玩!BAT指令碼
- Python字典的遍歷,包括key遍歷/value遍歷/item遍歷/Python
- OSS實現檔案下載進度條顯示
- 非遞迴實現先序遍歷和中序遍歷遞迴
- 批次影像識別的快速遍歷技巧
- js實現深度優先遍歷和廣度優先遍歷JS
- MySQL 實現樹形的遍歷MySql
- python實現二叉樹和它的七種遍歷Python二叉樹
- 二叉樹的遍歷實現二叉樹
- Python實現二叉樹的三種深度遍歷方法!Python二叉樹
- python字串遍歷方式Python字串
- python---字典遍歷Python
- 深度優先遍歷,廣度優先遍歷實現物件的深拷貝物件
- 使用Python實現網頁中圖片的批次下載和水印新增儲存Python網頁
- 刷題系列 - Python實現二叉樹按層級遍歷Python二叉樹
- python 實現二叉樹的深度&&廣度優先遍歷Python二叉樹
- python資料結構之二叉樹遍歷的實現Python資料結構二叉樹
- 利用Wireshark和OSS的API文件簡單實現上傳和下載API
- Python字典遍歷的陷阱Python
- Python中list的遍歷Python
- Python演算法:遍歷Python演算法
- linux下遍歷目錄Linux
- 圖的儲存與遍歷C++實現C++
- IEnumerable 介面 實現foreach 遍歷 例項
- Python爬蟲批次下載電影連結Python爬蟲
- 4.1 Python -- 遍歷整個列表Python
- python之 序列與字典遍歷Python
- python 遞迴遍歷目錄Python遞迴
- 刷題系列 - Python用非遞迴實現二叉樹前序遍歷Python遞迴二叉樹
- python實現批次執行命令列Python命令列
- 【JavaScript實用技巧(一)】迴圈遍歷與跳出迴圈遍歷JavaScript
- 層序遍歷樹的節點,佇列實現佇列
- 二叉樹的建立及遍歷(JavaScript實現)二叉樹JavaScript
- 遞迴遍歷樹狀結構優雅實現遞迴
- python實現郵件接收、附件下載Python
- js的map遍歷和array遍歷JS