基于python實(shí)現(xiàn)上傳文件到OSS代碼實(shí)例
基礎(chǔ)環(huán)境
# +++++ 阿里云OSS開發(fā)指南里都有詳細(xì)的步驟,在這里整理了一下自己需要的東西# 確定開發(fā)環(huán)境,centOS默認(rèn)安裝了python2.7# python -V# 安裝python開發(fā)包# yum install -y python-devel# 安裝OSS的sdk# yum install -y python-pip# pip2.7 install oss2# 驗(yàn)證oss2是否安裝正確’’’>>> import oss2>>> oss2.__version__’2.6.0’’’’# 驗(yàn)證OSS擴(kuò)展庫crcmod是否安裝’’’在python環(huán)境中,輸入一下內(nèi)容,如果有錯(cuò)誤信息,則說明擴(kuò)展庫安裝不成功,默認(rèn)安裝oss2的時(shí)候會(huì)安裝擴(kuò)展庫>>> import crcmod._crcfunext如果出現(xiàn)安裝不成功,則按一下步驟安裝:1、執(zhí)行以下命令卸載crcmod# pip uninstall crcmod2、安裝python-devel3、執(zhí)行以下命令重新安裝crcmod# pip install crcmod’’’
小文件上傳
#!/usr/bin/env python# -*- coding: utf-8 -*-import oss2# 阿里云主賬號(hào)AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問或日常運(yùn)維,請(qǐng)登錄 https://ram.console.aliyun.com 創(chuàng)建RAM賬號(hào)。auth = oss2.Auth(’<yourAccessKeyId>’, ’<yourAccessKeySecret>’)# Endpoint以杭州為例,其它Region請(qǐng)按實(shí)際情況填寫。bucket = oss2.Bucket(auth, ’http://oss-cn-hangzhou.aliyuncs.com’, ’<yourBucketName>’)# 必須以二進(jìn)制的方式打開文件,因?yàn)樾枰牢募淖止?jié)數(shù)。with open(’<yourLocalFile>’, ’rb’) as fileobj: # Seek方法用于指定從第1000個(gè)字節(jié)位置開始讀寫。上傳時(shí)會(huì)從您指定的第1000個(gè)字節(jié)位置開始上傳,直到文件結(jié)束。 fileobj.seek(1000, os.SEEK_SET) # Tell方法用于返回當(dāng)前位置。 current = fileobj.tell() bucket.put_object(’<yourObjectName>’, fileobj)
分片上傳
# -*- coding: utf-8 -*-import osfrom oss2 import SizedFileAdapter, determine_part_sizefrom oss2.models import PartInfoimport oss2# 阿里云主賬號(hào)AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問或日常運(yùn)維,請(qǐng)登錄 https://ram.console.aliyun.com 創(chuàng)建RAM賬號(hào)。auth = oss2.Auth(’<yourAccessKeyId>’, ’<yourAccessKeySecret>’)# Endpoint以杭州為例,其它Region請(qǐng)按實(shí)際情況填寫。bucket = oss2.Bucket(auth, ’http://oss-cn-hangzhou.aliyuncs.com’, ’<yourBucketName>’)key = ’<yourObjectName>’filename = ’<yourLocalFile>’total_size = os.path.getsize(filename)# determine_part_size方法用來確定分片大小。part_size = determine_part_size(total_size, preferred_size=100 * 1024)# 初始化分片。upload_id = bucket.init_multipart_upload(key).upload_idparts = []# 逐個(gè)上傳分片。with open(filename, ’rb’) as fileobj: part_number = 1 offset = 0 while offset < total_size: num_to_upload = min(part_size, total_size - offset)# SizedFileAdapter(fileobj, size)方法會(huì)生成一個(gè)新的文件對(duì)象,重新計(jì)算起始追加位置。 result = bucket.upload_part(key, upload_id, part_number, SizedFileAdapter(fileobj, num_to_upload)) parts.append(PartInfo(part_number, result.etag)) offset += num_to_upload part_number += 1# 完成分片上傳。bucket.complete_multipart_upload(key, upload_id, parts)# 驗(yàn)證分片上傳。with open(filename, ’rb’) as fileobj: assert bucket.get_object(key).read() == fileobj.read()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. ASP實(shí)現(xiàn)加法驗(yàn)證碼3. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 詳解JSP 內(nèi)置對(duì)象request常見用法6. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算7. Python matplotlib 繪制雙Y軸曲線圖的示例代碼8. jsp EL表達(dá)式詳解9. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除10. springboot集成與使用Sentinel的方法
