【发布时间】:2018-01-20 17:01:22
【问题描述】:
我目前正在开发一个网络应用程序,该应用程序将使用 Google Cloud Storage 上传用户文档。然而,我遇到了障碍。当我尝试继续上传时,我不断收到[Errno 2] No such file or directory: 'Marty McFly-Paycheck-06_ConditionalDirectives.pdf'。这个错误让我很困惑,因为我认为该文件将直接从我的表单文件上传中获取。
我的假设错了吗?我应该保存然后以某种方式提供文件吗?
将文档上传到 Cloud Storage 的当前代码
#Uploading Documents function
def upload_documents(bucket_name,source_file_name,destination_blob_name):
storage_client = storage.Client()
storage_bucket = storage_client.get_bucket(bucket_name)
blob = storage_bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print("Document Uploaded")
from .forms import DocumentUpload
# Create your views here.
def get_upload(request):
# If this is a POST request, we will process the data -- sending it to Google Cloud Storage
if request.method == 'POST':
#create a form instance populated with data -- (Bound to the data previously entered that might need correction)
name = request.POST['name']
titleofdocument = request.POST['titleofdocument']
doc_file = request.POST['doc_file']
fullUpload = name + "-" + titleofdocument + "-" + doc_file
print(titleofdocument,name,doc_file)
print(fullUpload)
form = DocumentUpload(request.POST)
upload_documents("upload_documents",fullUpload,"SampleBlob")
else:
form = DocumentUpload()
return render(request, 'document-upload.html',{'form':form})
有问题的全栈跟踪
Marty McFly-Paycheck-06_ConditionalDirectives.pdf
Internal Server Error: /upload-documents/
Traceback (most recent call last):
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Joel\Documents\dev-projects\greaterdemand\documents\views.py", line 40, in get_upload
upload_documents("upload_documents",fullUpload,"SampleBlob")
File "C:\Users\Joel\Documents\dev-projects\greaterdemand\documents\views.py", line 23, in upload_documents
blob.upload_from_filename(source_file_name)
File "C:\Users\Joel\Documents\dev-projects\django-env\lib\site-packages\google\cloud\storage\blob.py", line 988, in upload_from_filename
with open(filename, 'rb') as file_obj:
FileNotFoundError: [Errno 2] No such file or directory: 'Marty McFly-Paycheck-06_ConditionalDirectives.pdf'
[20/Jan/2018 10:54:03] "POST /upload-documents/ HTTP/1.1" 500 78673
如果有人有任何指向有用文档或建议的链接,我将不胜感激。谢谢!
【问题讨论】:
-
你需要传递文件。
form = DocumentUpload(request.POST)应该是form = DocumentUpload(request.POST, request.FILES)。
标签: python django django-forms google-cloud-storage