上传一个h5py文件到aws并重新下载

Uploading a h5py file to aws and downloading it again

我还没有找到明确的解决方案,所以希望这个问题将来对其他人有用。

基本上我正在创建一个 h5py 对象,如下所示:

keys_d = [matrix_l2T.data, matrix_l2T.indices, matrix_l2T.indptr]
keys_n = ['data', 'indices', 'indptr' ]
file_to_save = h5py.File(FILENAME)
for i,j in zip(keys_n, keys_d):
    file_to_save.create_dataset(i, data = j)

上传到AWS如下:

s3 = boto3.client('s3')
s3.upload_file(Filename = FILENAME, Key = KEY, Bucket = BUCKET)

下载如下:

s3 = boto3.resource('s3')
try:
s3.Bucket(BUCKET_NAME).download_file(KEY, FILENAME) except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
    print("The object does not exist.")
else:
    raise

然后我试过用各种方法打开这个文件,但似乎都没有正确执行。

我试过:

h5py.File(FILENAME,'r')

我得到了:

OSError: Unable to open file (bad object header version number)

我也试过:

with open(FILENAME, 'rb') as f:
   a = f.read()

我需要 rb,因为没有它我会得到 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte。最后一段代码 returns 二进制代码不太确定我下一步应该做什么。

我解决了这个问题。我没有关闭 h5py 文件。需要添加

file_to_save.close()

上传到 aws 之前