我正在完成一项大学作业,其中步骤之一是使用 Cloud9 IDE 将对象上传到 S3 存储桶。在尝试了我的主代码后,我做了一些研究,发现了一个 YouTube 视频教程,我复制了下面的代码。
即使明确创建了文件,此代码仍然会抛出“无文件或目录”错误。我尝试直接指定路径并移动文本文档,但似乎没有任何效果。我不知道还能做什么,似乎无论我指向哪个目录,cloud9 都说那里什么也没有。
import os
import boto3
client = boto3.client('s3')
bucket = 'fname-lname-102823'
cur_path = os.getcwd()
file = 'hw1-object.txt'
filename = os.path.join(cur_path, 'data', file)
print(filename)
data = open(filename, 'rb')
client.upload_file(filename, bucket, file)
***Screenshot of C9 IDE showing file exists***
(https://i.stack.imgur.com/XrrgP.png)```
根据您的文件结构,这是不正确的:
filename = os.path.join(cur_path, 'data', file)
data
文件夹不存在;尝试:
filename = os.path.join(cur_path, file)