Hi everyone, can you help me?
Thanks in advance

Thread: 
https://stackoverflow.com/questions/45449102/django-python-oserror-no-such-file-or-directory-but-file-exists

Code:



down votefavorite 
<https://stackoverflow.com/questions/45449102/django-python-oserror-no-such-file-or-directory-but-file-exists#>

I'm converting doc and docx files to pdf in the server using unoconv with 
LibreOffice. And I need to upload to S3 the converted file.

I can convert with success the files and I can see them in the server.

But when I try to upload the pdf, I get the error. What am I missing?

Thanks in advance

This works just fine:

import subprocessfrom 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()

This is the code in which I get the error in line: 
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)


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7cecdcdb-7fcf-4f4a-858a-30801fa9cf9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to