pythonalibaba-cloudalibaba-cloud-oss

Save DataFrame to CSV or Text to Alicloud OSS


I will like to know how to export pandas dataframe as csv/txt file to Alicloud OSS. From the documentation in https://www.alibabacloud.com/help/en/doc-detail/88426.html

the closest way I can find is to export it as csv/txt locally on my computer and copy the file to OSS. E.g.

import oss2
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
bucket.put_object_from_file('exampleobject.txt', 'D:\\localpath\\examplefile.txt')

Hence will like to know if there is a way to export the file directly to OSS, without the need to export to my computer first. Thank you!


Solution

  • Managed to solve this in the end. Exporting it as csv without the filename using Pandas library will convert the dataframe in python to text without exporting the dataframe.

    bucket.put_object('example.txt', df.to_csv(index = False, encoding = 'utf-8-sig'))