Python 3.x 我正在使用IBM云对象存储,希望从存储中读取pdf文件,并希望以字符串形式存储其文本内容
我使用了ibm COS文档中提到的ibm_boto3。我对资源的定义如下:Python 3.x 我正在使用IBM云对象存储,希望从存储中读取pdf文件,并希望以字符串形式存储其文本内容,python-3.x,ibm-cloud,ibm-cloud-storage,Python 3.x,Ibm Cloud,Ibm Cloud Storage,我使用了ibm COS文档中提到的ibm_boto3。我对资源的定义如下: cos = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=SERVICE_INSTANCE_ID, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oaut
cos = ibm_boto3.resource("s3",
ibm_api_key_id=COS_API_KEY_ID,
ibm_service_instance_id=SERVICE_INSTANCE_ID,
ibm_auth_endpoint=COS_AUTH_ENDPOINT,
config=Config(signature_version="oauth"),
endpoint_url=COS_ENDPOINT
)
以下是我用来获取pdf文件内容的代码:
def get_item(bucket_name, item_name):
print("Retrieving item from bucket: {0}, key: {1}".format(bucket_name, item_name))
try:
file = cos.Object(bucket_name, item_name).get()
file_content = file["Body"].read() #returns data in bytes
#print("\nFILE:-------------------------\n", file) #shows the meta data of the object
return file_content
except ClientError as be:
print("CLIENT ERROR: {0}\n".format(be))
except Exception as e:
print("Unable to retrieve file contents: {0}\n".format(e))
对象的类型为ibm_botocore.response.StreamingBody对象。
我无法将以字节为单位获得的数据转换为字符串。我曾尝试使用utf-8和base64进行解码,但没有成功。当我尝试使用utf-8进行解码时,出现以下错误:
无法检索文件内容:“utf-8”编解码器无法解码位置11中的字节0xb5:无效的开始字节
我也无法确定IBMCOS使用的是什么类型的编码
提前谢谢。你到处找过了吗?COS正在使用S3。试过你吗