Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

python 3.x - The file is uploading in s3bucket but not inserting the file name in table

Hi i am uploading a file in s3bucket thru python api,the file is uploading in s3bucket but not inserting the file name in table. i am sending a input parameter "attachments" as json object,in attachments object have the below format {"projects_attachment_name":"sign up.PNG", "projects_id":2, "task_id":0, "created_by":1038, "created_date":"01/09/2020, "status":true } i am sending the input parameters are in the picture below the my code,i am getting error **'str' object does not support item assignment i am sending the input parameters are in the picture

def upload_to_aws(filedata, filename, timestamp, company_id,folder_path,attachments):
            s3 = boto3.client('s3', aws_access_key_id=settings.S3_BUCKET_BUCKET_AccessKeyId,
                              aws_secret_access_key=settings.S3_BUCKET_BUCKET_SecretAccessKey)

            try:
                file_name, extension = splitext(filename)
                curntfilename = file_name + str(timestamp) + extension

                # curntfilename = filedata.name
                isexist = False
                for key in s3.list_objects(Bucket=settings.S3_BUCKET_BUCKET_NAME)['Contents']:

                    folder = key['Key']
                    if folder == (folder_path):
                        isexist = True
                        data = s3.upload_fileobj(filedata, settings.S3_BUCKET_BUCKET_NAME,'%s/%s' % (folder_path, curntfilename))
                        uploadprofile(curntfilename, company_id,result[0])
                        return HttpResponse(json.dumps(data))
                if isexist == False:
                    s3.upload_fileobj(filedata, settings.S3_BUCKET_BUCKET_NAME,
                                      '%s/%s' % (folder_path, curntfilename))
                    uploadprofile(curntfilename, company_id,attachments)
                print("Upload Successful")
                return True
            except FileNotFoundError:
                print("The file was not found")
                return False
            except NoCredentialsError:
                print("Credentials not available")
                return False


def uploadprofile(curntfilename, company_id,attachments):
    try:
        company_id = company_id
        execute_function = query_executer(company_id)
        attachments['projects_attachment_name'] = curntfilename
        # attachments = request.data.get('attachments')
        params = {'attachments': json.dumps(attachments)}
        result = execute_function.put(params, db_functions.fn_project_management_attachment)
        return HttpResponse(result)
    except Exception as err:
        http_err = traceback.format_exc()
        print(http_err)
        return HttpResponse(http_err, status=500)
    finally:
        execute_function.close_db_connection()
question from:https://stackoverflow.com/questions/65662089/the-file-is-uploading-in-s3bucket-but-not-inserting-the-file-name-in-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...