基于python實(shí)現(xiàn)上傳文件到OSS代碼實(shí)例
基礎(chǔ)環(huán)境
# +++++ 阿里云OSS開(kāi)發(fā)指南里都有詳細(xì)的步驟,在這里整理了一下自己需要的東西# 確定開(kāi)發(fā)環(huán)境,centOS默認(rèn)安裝了python2.7# python -V# 安裝python開(kāi)發(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ò)展庫(kù)crcmod是否安裝’’’在python環(huán)境中,輸入一下內(nèi)容,如果有錯(cuò)誤信息,則說(shuō)明擴(kuò)展庫(kù)安裝不成功,默認(rèn)安裝oss2的時(shí)候會(huì)安裝擴(kuò)展庫(kù)>>> 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的訪問(wèn)權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問(wèn)或日常運(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)制的方式打開(kāi)文件,因?yàn)樾枰牢募淖止?jié)數(shù)。with open(’<yourLocalFile>’, ’rb’) as fileobj: # Seek方法用于指定從第1000個(gè)字節(jié)位置開(kāi)始讀寫。上傳時(shí)會(huì)從您指定的第1000個(gè)字節(jié)位置開(kāi)始上傳,直到文件結(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的訪問(wèn)權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問(wèn)或日常運(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方法用來(lái)確定分片大小。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. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)2. Springboot 全局日期格式化處理的實(shí)現(xiàn)3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. python 浮點(diǎn)數(shù)四舍五入需要注意的地方5. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法6. idea配置jdk的操作方法7. VMware中如何安裝Ubuntu8. JAMon(Java Application Monitor)備忘記9. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題10. Docker容器如何更新打包并上傳到阿里云
