我可以直接使用 InMemoryUploadedFile 将图像上传到 Imgur 吗?
Could I use InMemoryUploadedFile directly to upload Image to Imgur?
我在使用 Django 上传图片到 Imgur 时遇到了一些问题。
我已经完成了从react上传的图像文件,并通过axios将文件数据传递给后端。
但是我从 Python 后端的 request.data 得到的对象是 InMemoryUploadedFile
.
我不想在磁盘中存储任何图像文件。
我可以直接使用这个InMemoryUploadedFile
类型的文件通过Imgur上传功能上传upload_from_path
吗?
如果可以,我该怎么办?
这是我的代码,
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from django.contrib.auth.models import User
from base.models import InCome, IncomeMoneyCategory, IncomeContributeContext,OutcomeMoneyCategory, OutcomeContributeContext, Member, Student, OutCome
from django.db.models import F, Sum
from base.serializers import IncomeSerializer, OutcomeSerializer
from rest_framework import status
from datetime import datetime
import configparser
import base.config as env
import os
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
from imgurpython import ImgurClient
@api_view(['POST'])
def upload_image(request):
data = request.data
print(data)
album = env.IMGUR_ALBUM
print(data['image'])
image=data['image']
print("--")
print(type(image))
image_path=image
config = configparser.ConfigParser()
path = '/'.join((os.path.abspath(__file__).replace('\', '/')).split('/')[:-1])
config.read(os.path.join(path, 'auth.ini'))
#config.read('auth.ini')
client_id = config['credentials']['client_id']
client_secret = config['credentials']['client_secret']
refresh_token = config['credentials']['refresh_token']
access_token = config['credentials']['access_token']
client = ImgurClient(client_id,client_secret, refresh_token)
client.set_user_auth(access_token, refresh_token)
if client:
config={
'album':album,
'name':'Ezra',
'title':'Test',
'description': 'Test {0}'.format(datetime.now())
}
print("Uploading image")
image = client.upload_from_path(str(image_path),config=config,anon=False)
print(image['link'])
print("Done")
return image
else:return "Error"
image
的类型是<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
。
但是imgurpython包的函数ImgurClient.upload_from_path
第一个参数需要上传的文件名
所以当我执行这段代码时,我会得到这个错误:
No such file or directory:...
希望有人能帮我解决,对不起我的英语不好。
感谢阅读。
后来解决了
我使用default_storage
保存文件,并在upload_from_path
方法中将目录作为第一个参数。
上传到 Imgur 后,我删除了文件并 return link 返回。
这是我的代码:
def upload_image(request):
data = request.data
album = env.IMGUR_ALBUM
image=data['image']
file = data['image']
filename = default_storage.save(file.name, ContentFile(file.read()))
config = configparser.ConfigParser()
path = '/'.join((os.path.abspath(__file__).replace('\', '/')).split('/')[:-1])
config.read(os.path.join(path, 'auth.ini'))
client_id = config['credentials']['client_id']
client_secret = config['credentials']['client_secret']
refresh_token = config['credentials']['refresh_token']
access_token = config['credentials']['access_token']
client = ImgurClient(client_id,client_secret, refresh_token)
client.set_user_auth(access_token, refresh_token)
if client:
config={
'album':album,
'name':'Ezra',
'title':'Test',
'description': 'Test {0}'.format(datetime.now())
}
print("Uploading image")
image = client.upload_from_path(settings.MEDIA_ROOT+'/'+filename,config=config,anon=False)
print("Done")
default_storage.delete(filename)
return Response(image['link'])
else:return "Error"
我在使用 Django 上传图片到 Imgur 时遇到了一些问题。
我已经完成了从react上传的图像文件,并通过axios将文件数据传递给后端。
但是我从 Python 后端的 request.data 得到的对象是 InMemoryUploadedFile
.
我不想在磁盘中存储任何图像文件。
我可以直接使用这个InMemoryUploadedFile
类型的文件通过Imgur上传功能上传upload_from_path
吗?
如果可以,我该怎么办?
这是我的代码,
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from django.contrib.auth.models import User
from base.models import InCome, IncomeMoneyCategory, IncomeContributeContext,OutcomeMoneyCategory, OutcomeContributeContext, Member, Student, OutCome
from django.db.models import F, Sum
from base.serializers import IncomeSerializer, OutcomeSerializer
from rest_framework import status
from datetime import datetime
import configparser
import base.config as env
import os
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
from imgurpython import ImgurClient
@api_view(['POST'])
def upload_image(request):
data = request.data
print(data)
album = env.IMGUR_ALBUM
print(data['image'])
image=data['image']
print("--")
print(type(image))
image_path=image
config = configparser.ConfigParser()
path = '/'.join((os.path.abspath(__file__).replace('\', '/')).split('/')[:-1])
config.read(os.path.join(path, 'auth.ini'))
#config.read('auth.ini')
client_id = config['credentials']['client_id']
client_secret = config['credentials']['client_secret']
refresh_token = config['credentials']['refresh_token']
access_token = config['credentials']['access_token']
client = ImgurClient(client_id,client_secret, refresh_token)
client.set_user_auth(access_token, refresh_token)
if client:
config={
'album':album,
'name':'Ezra',
'title':'Test',
'description': 'Test {0}'.format(datetime.now())
}
print("Uploading image")
image = client.upload_from_path(str(image_path),config=config,anon=False)
print(image['link'])
print("Done")
return image
else:return "Error"
image
的类型是<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
。
但是imgurpython包的函数ImgurClient.upload_from_path
第一个参数需要上传的文件名
所以当我执行这段代码时,我会得到这个错误:
No such file or directory:...
希望有人能帮我解决,对不起我的英语不好。 感谢阅读。
后来解决了
我使用default_storage
保存文件,并在upload_from_path
方法中将目录作为第一个参数。
上传到 Imgur 后,我删除了文件并 return link 返回。
这是我的代码:
def upload_image(request):
data = request.data
album = env.IMGUR_ALBUM
image=data['image']
file = data['image']
filename = default_storage.save(file.name, ContentFile(file.read()))
config = configparser.ConfigParser()
path = '/'.join((os.path.abspath(__file__).replace('\', '/')).split('/')[:-1])
config.read(os.path.join(path, 'auth.ini'))
client_id = config['credentials']['client_id']
client_secret = config['credentials']['client_secret']
refresh_token = config['credentials']['refresh_token']
access_token = config['credentials']['access_token']
client = ImgurClient(client_id,client_secret, refresh_token)
client.set_user_auth(access_token, refresh_token)
if client:
config={
'album':album,
'name':'Ezra',
'title':'Test',
'description': 'Test {0}'.format(datetime.now())
}
print("Uploading image")
image = client.upload_from_path(settings.MEDIA_ROOT+'/'+filename,config=config,anon=False)
print("Done")
default_storage.delete(filename)
return Response(image['link'])
else:return "Error"