原因很可能是安全组未正确配置,需要添加对应的入站规则,允许HTTP请求或HTTPS请求。另外,也需要检查代码逻辑是否正确,是否正确指定了上传文件的路径。以下是示例代码:
import boto3
from botocore.exceptions import NoCredentialsError
def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3')
try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
uploaded = upload_to_aws('path/to/local/file', 'bucket-name', 's3-file-name')
注意修改参数:local_file为本地文件路径,bucket为存储桶名称,s3_file为S3中的文件路径。