简体   繁体   English

使用Python的IBM对象存储

[英]IBM Object Storage with Python

I have some issue trying to save a file to IBM object storage via python. 尝试通过python将文件保存到IBM对象存储时遇到一些问题。 I copied the following credentials from the bluemix account (with details omitted below). 我从bluemix帐户复制了以下凭证(以下省略了详细信息)。

credentials = {
  "auth_url": "https://identity.open.softlayer.com",
  "project": <my project>,
  "projectId": <my project id>,
  "region": "dallas",
  "userId": <user id>,
  "username": <user name>,
  "password": <password>,
  "domainId": <domain Id>,
  "domainName": <domain Name>,
  "role": <role>
  }

And below is the python script that I have used trying to save a file to the container from io import StringIO import requests import json 下面是我用来尝试将文件从io import String保存到容器的python脚本IO导入请求import json

url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'],
        'password': {'user': {'name': credentials['username'],'domain': {'id': credentials['domainId']},
        'password': credentials['password']}}}}}
headers1 = {'Content-Type': 'application/json'}
resp1 = requests.post(url=url1, data=json.dumps(data), headers=headers1)
resp1_body = resp1.json()
for e1 in resp1_body['token']['catalog']:
    if(e1['type']=='object-store'):
        for e2 in e1['endpoints']:
                    if(e2['interface']=='public'and e2['region']=='dallas'):
                        url2 = ''.join([e2['url'],'/', container, '/', filename])
s_subject_token = resp1.headers['x-subject-token']
headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
print(url2)
resp2 = requests.post(url=url2, data=filename, headers=headers2)
print(resp2.text)
return StringIO(resp2.text)

filename = "sample.png"<br>
post_object_storage_file("democontainer", filename)

I seems to get a token via resp1 and obtained url2. 我似乎通过resp1获得了令牌并获得了url2。 However, I get a 'Forbidden' response when i print resp2.text. 但是,当我打印resp2.text时,收到“禁止”响应。 I am the admin for that storage container so I don't see why I can't have access with this. 我是该存储容器的管理员,所以我看不到为什么无法使用此存储容器。

I am new to the IBM object storage so any advice will be helpful. 我是IBM对象存储的新手,所以任何建议都将对您有所帮助。

Thanks. 谢谢。

The code you have is for reading objects from storage. 您拥有的代码用于从存储中读取对象。

I would recommend you to use the "insert credentials" option in the Data Science Experience Data Import Panel and then use the swift client to save and read files from object storage. 我建议您使用Data Science Experience数据导入面板中的“插入凭据”选项,然后使用swift客户端保存和读取对象存储中的文件。 With Data Science Experience you can't reference files from your local hard disk, therefore I have given example of an image retrieved from the web. 借助Data Science Experience,您无法从本地硬盘中引用文件,因此,我举了一个从Web检索图像的示例。

Swift Client has the put_object function for saving objects. Swift Client具有put_object函数,用于保存对象。

import swiftclient
import keystoneclient.v3 as keystoneclient
from PIL import Image   ## To get image
import requests         ## To get image
from io import BytesIO


credentials = {
   ## Credentials HERE
}

conn = swiftclient.Connection(
key=credentials['password'],
authurl=credentials['auth_url']+"/v3",
auth_version='3',
os_options={
    "project_id": credentials['project_id'],
    "user_id": credentials['user_id'],
    "region_name": credentials['region']})



response = requests.get("URL to image file") ## Change to link to image file
img = Image.open(BytesIO(response.content))

conn.put_object(credentials['container'],"test.jpg",response,content_type='image/jpeg')  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IBM Cloud Object 存储连接问题 Watson studio Python 笔记本 - IBM Cloud Object Storage Connection issue Watson studio Python notebook 无法使用 python 在 IBM Cloud object 存储中检索存储桶名称 - Cannot retrieve bucket name in IBM Cloud object storage using python 使用 python 将文件上传到 IBM 云 object 存储时出错 - error while uploading file to IBM cloud object storage using python 尝试使用 Python 列出 IBM Cloud Object Storage 中的对象时“指定的存储桶不存在” - "The specified bucket does not exist" when trying to list objects in IBM Cloud Object Storage using Python 如何使用Python从IBM Cloud Object Storage中读取Parquet文件的元数据? - How to read Parquet file's metadata from IBM Cloud Object Storage in Python? Python:如何为IBM Cloud Object Storage生成预签名URL? - Python: How do I generate presigned URLs for IBM Cloud Object Storage? 我如何使用本地python代码从ibm对象存储中获取容器 - how do i get a container from ibm object storage using a local python code 如何从存储桶中获取所有文件 - IBM Cloud Object Storage? - How to get all files from a Bucket - IBM Cloud Object Storage? Python天蓝色存储实体对象 - python azure storage entity object 软件层对象存储Python API的生存时间 - Softlayer Object Storage Python API Time To Live
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM