Background:
In the past, Huawei OBS was used to download pictures (that is, to view pictures through a browser), and the address was used to access OBS directly.
for example,
endpoint: obs-example-domain.cn
picture name: QCX% 2F1% 2f20210804% 2f2db3c4bb-0c2c-4c3c-84e0-7e131c1e8db61628047890560.jpg
Access address:
http://obs-example-domain.cn/qcx%2F1%2F20210804%2F2db3c4bb -0c2c-4c3c-84e0-7e131c1e8db61628047890560. jpg
WGet can download pictures from the above address.
However, if you use Python SDK to access, an error will be reported:
AK = 'PLAU4DD8EYVXSA****UL'
SK = 'MdNZCKgSwt9Qgq6ZXtaF7wtZOd8********xEiv'
server = "http://obs-example-domain.cn"
bucketName = 'qcx'
obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server)
name = "qcx%2F1%2F20210804%2F2db3c4bb-0c2c-4c3c-84e0-7e131c1e8db61628047890560.jpg"
resp = obsClient.getObject(bucketName, name, loadStreamInMemory=True)
print(resp.body)
Output: the specified key does not exist
In the above procedures: name = QCX% 2F1% 2f20210804% 2f2db3c4bb-0c2c-4c3c-84e0-7e131c1e8db61628047890560 jpg
Solution:
The picture name above is actually URLEncode. The original string urlcode can obtain:
qcx/1/20210804/2db3c4bb-0c2c-4c3c-84e0-7e131c1e8db61628047890560. jpg
Change the name in the above code to the picture name after URLDecode, that is:
name = "qcx/1/20210804/2db3c4bb-0c2c-4c3c-84e0-7e131c1e8db61628047890560.jpg"
You can get the picture correctly.
The function completion code
Picture browsing completed line:
Browser request img URL -> nginx -> API (with SK AK) – > obs -> Response API – > nginx -> browser
Python 2.7 (only this version is available on the server and cannot be upgraded to 3.X)
#coding=utf-8
from BaseHTTPServer import BaseHTTPRequestHandler
import urllib
import cgi
import os
import urllib
# print urllib.unquote('%E4%B8%89%E7%94%9F%E4%B8%89%E4%B8%96')
from obs import ObsClient
AK = 'PLAU4DD8EYVXSA****UL'
SK = 'MdNZCKgSwt9Qgq6ZXtaF7wtZOd8********xEiv'
server = "http://obs.cn-dchlw-1.digitalgd.com.cn"
bucketName = 'qcxx'
obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server)
cwd = os.getcwd()
class ObsHandler(BaseHTTPRequestHandler):
def do_GET(self):
path = urllib.unquote(self.path)
buf = ""
query_list = path.split("/") #auto urldecode
#print(query_list)
if query_list[1] == "obs":
name = "/".join(query_list[3:]).replace("?","") # for online
#name = urllib.unquote(name)
resp = obsClient.getObject(bucketName, name, loadStreamInMemory=True)
if resp.status < 300:
self.send_response(200)
buf = resp.body.buffer
else:
self.send_response(400)
self.end_headers()
self.wfile.write(buf)
def StartServer():
from BaseHTTPServer import HTTPServer
sever = HTTPServer(("",12000),ObsHandler)
sever.serve_forever()
if __name__=='__main__':
StartServer()
Read More:
- Python: How to get the size of the picture (byte/kb/mb)
- [Solved] pycharm Import New Project Error: cannot set up a python sdk
- [Solved] Windows10 Pycharm Use Virtual Environment Error: Cannot set up a python SDK
- [Python] Right-click Selenium to Save the picture error: attributeerror: solution to module ‘pyscreen’ has no attribute ‘locationonwindow’
- CV: How to extracts the part of the picture with the specified color
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Nltk Library Download error: [errno: 11004] getaddrinfo failed
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] Pytorch Download CIFAR1 Datas Error: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certi
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- How to Fix “HTTP error 403: forbidden” in Python 3. X
- Linux installs Python and upgrades Python
- python minio client Error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certific
- The anaconda-navigator interface cannot be started, loading application, report ERROR download_api._get_url error
- Python 3 urllib has no URLEncode attribute
- Python Error: pip install mysql-connector-python failed
- Python: How to Disable InsecureRequestWarning error
- Python raspberry pie starts sending IP address to mailbox
- [Solved] Python Error: tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.