[Solved] Huawei OBS Python SDK download picture error: nosuchkey

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: