2017-08-01 113 views
0

我正在將doc和docx文件轉換爲使用unoconv與LibreOffice的服務器中的pdf。我需要上傳到S3轉換後的文件。Django Python OSError沒有這樣的文件或目錄,但存在文件

我可以轉換成功的文件,我可以看到他們在服務器中。

但是當我嘗試上傳PDF時,出現錯誤。我錯過了什麼?

在此先感謝

這只是正常:

import subprocess 
from boto.s3.connection import S3Connection, Bucket, Key 

def doc_to_pdf(user): 
    ''' 
    Convert doc or docx to PDF. 

    parameter user: is a request.user 

    Usage: 
     doc_to_pdf(self.request.user): 
    ''' 

    user_cv = CandidateCV.objects.get(user=user) 
    user_cv_file = str(user_cv.resume).split('/')[-1] # tem que ser PDF 
    user_cv_filetype = user_cv_file.split('.')[-1] 

    if not user_cv_filetype in settings.PDF_FILE_TYPE: 
     # Se não for PDF 
     file_in = user_cv.resume.url 
     file_name = file_in.split('/')[-1] 
     # download 
     urllib.request.urlretrieve(file_in, file_name) 
     file_out = user_cv_file.split('.')[0] + '.pdf' 

     # converte para PDF 
     env = os.environ.copy() 
     env['HOME'] = '/tmp' 
     subprocess.Popen(["unoconv","-f", "pdf", "%s" % (file_in)], env = env) 

     # Define a path para salvar o documento na S3 
     resume_path = 'resumes/%s/' % str(date.today()) 

     # key é o nome do arquivo na S3 
     key = '%s%s' % (resume_path, file_out) 

     # deleta o arquivo localmente 
     subprocess.call("rm -f %s" % user_cv_file, shell=True) 

     # Salva o novo formato no banco 
     user_cv.resume = key 
     user_cv.save() 

這是我得到的錯誤在一行代碼:k_out.set_contents_from_filename(s3file)

def s3upload(s3file): 

    # Conecta na AWS S3 
    conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) 
    bucket_out = Bucket(conn, settings.AWS_STORAGE_BUCKET_NAME) 
    k_out = Key(bucket=bucket_out, name=s3file) 

    # Define a path para salvar o documento na S3 
    resume_path = 'resumes/%s/' % str(date.today()) 

    # key é o nome do arquivo na S3 
    key = '%s%s' % (resume_path, s3file) 
    k_out.key = key 

    # Salva na AWS S3 
    k_out.set_contents_from_filename(s3file) 
    k_out.make_public() 

    # deleta o arquivo localmente 
    subprocess.call("rm -f %s" % s3file, shell=True) 

這裏是回溯:

IOError at /pt-br/dashboard/jobcombo/25-mnj70998-809898m-nh8/candidate-base/ 
[Errno 2] No such file or directory: '/opt/python/bundle/42/app/da15ad64-ef23-47ff-b6f0-f2f8e0cdc2c2.pdf' 

Traceback: 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    132.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/views/generic/base.py" in view 
    71.    return self.dispatch(request, *args, **kwargs) 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper 
    34.    return bound_func(*args, **kwargs) 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    22.     return view_func(request, *args, **kwargs) 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func 
    30.     return func.__get__(self, type(self))(*args2, **kwargs2) 
File "/opt/python/current/app/combo/views.py" in dispatch 
    3190.   return super(CandidateBase, self).dispatch(*args, **kwargs) 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch 
    89.   return handler(request, *args, **kwargs) 
File "/opt/python/current/app/combo/views.py" in post 
    3560.      s3upload(user_cv_file) 
File "/opt/python/current/app/combo/aws_upload.py" in s3upload 
    107.  k_out.set_contents_from_filename(file_to_upload) 
File "/opt/python/run/venv/lib/python2.7/site-packages/boto/s3/key.py" in set_contents_from_filename 
    1370.   with open(filename, 'rb') as fp: 
+0

這會更有助於顯示錯誤的追溯。 – Bobort

+0

我該怎麼做?日誌文件? –

+0

只需複製並粘貼Python在命令行上吐出的內容即可。它應該以'Traceback(最近調用最後一個)'開頭:'如果你沒有命令提示符,那麼日誌文件應該有相同的文本。 – Bobort

回答

1

結果unoconv需要2秒來執行文件轉換。 所以對於文件的轉換,我必須設置:

p = subprocess.Popen('blah') 
p.wait() 

而且1周後我得到了這個工作。

謝謝

相關問題