Comments (8)
Regarding url method you might just get the URL and if your bucket is private you will get your presigned URL with valid time according to your settings MINIO_URL_EXPIRY_HOURS
from django-minio-backend.
@Podolyan98 you can get the url by accessing .file.url
. in your case it would be either photo.file.url
or license.file.url
.
Using an example from DjangoExampleApplication:
from DjangoExampleApplication.models import PrivateAttachment
pa = PrivateAttachment.objects.first()
print(pa.file.url)
'https://play.min.io/xxxxx/2022-12-17/user/xxxx.png?X-Amz-Algorithm=(........)'
from django-minio-backend.
Thanks for stepping in, @a13xg0 !
I'm closing this now. If you're still experiencing problems, @ghorbani-mohammad, please open another issue!
from django-minio-backend.
@a13xg0 Hello. Through the Django admin panel the URL method works, but through the my API I only see the direct path:
GET http://localhost:8888/api/users/aa658d18-e0b0-4537-8cb6-7e3c2251c197
Response:
{
"id": "aa658d18-e0b0-4537-8cb6-7e3c2251c197",
"photo": "aa658d18-e0b0-4537-8cb6-7e3c2251c197/2022-12-16_11-34-01.png",
"license": "aa658d18-e0b0-4537-8cb6-7e3c2251c197/file.md",
How to get pre-signed url?
from django-minio-backend.
@Podolyan98 Hi! My first guess looking at your response, is that you just got the value from your DB, not the resolved URL thru the minio's backend. You may check the order of middleware in your config.
from django-minio-backend.
@a13xg0 How can I get the resolved url through the minio backend?
models.py:
photo = models.ImageField(storage=MinioBackend(bucket_name='django-backend-users-private', replace_existing=True), upload_to=user_directory_path, null=True, blank=True)
license = models.FileField(storage=MinioBackend(bucket_name='django-backend-users-private', replace_existing=True), upload_to=user_directory_path, null=True, blank=True)
settings.py:
MINIO_ENDPOINT = 'docs.domain.com'
MINIO_EXTERNAL_ENDPOINT = 'docs.domain.com'
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS = True
MINIO_ACCESS_KEY = '123'
MINIO_SECRET_KEY = '123'
MINIO_USE_HTTPS = True
MINIO_URL_EXPIRY_HOURS = timedelta(days=1)
MINIO_PRIVATE_BUCKETS = [
'django-backend-dev-private',
'django-backend-users-private',
]
MINIO_POLICY_HOOKS: List[Tuple[str, dict]] = []
MINIO_BUCKET_CHECK_ON_SAVE = True
from django-minio-backend.
@theriverman In models.py
I specified the method that is used to get the private link in the APIView.
models.py
:
photo_0 = models.ImageField(storage=MinioBackend(bucket_name='django-backend-users-private', replace_existing=True), upload_to=User_directory_path('photo_0'), null=True, blank=True)
@property
def photo_0_url(self):
if self.photo_0 and hasattr(self.photo_0, 'url'):
return self.photo_0.url
views.py
:
def get(self, request, pk):
try:
queryset = User.objects.filter(id=pk).first()
user_dict = queryset.values().first()
# overwrite file path using resolved URL thru the minio's backend
user_dict['photo_0'] = user.photo_0_url
return Response(user_dict, status=status.HTTP_200_OK)
except Exception as error:
logger.error(traceback.format_exc())
raise APIException(error)
It works. The problem is that there are a lot of fields like ImageField
and FileField
, I need to overwrite each field in views.py
, for example user_dict['photo_0'] = user.photo_0_url
. Is it possible to call similar methods automatically for every ImageField
or FileField
?
By the way, when creating or updating records with ImageField
and FileField
, private links are automatically generated.
from django-minio-backend.
@Podolyan98 I don't see how your question is related to this third-party package.
What you're asking is more of a generic Django question which should not be discussed here.
from django-minio-backend.
Related Issues (20)
- Import timedelta in models.py HOT 1
- ModuleNotFoundError for latest Minio version (>6.0) HOT 4
- Why is it necessary to have both public and private buckets? HOT 1
- OSError at HOT 1
- For public files, click on their link does not start download immediately HOT 4
- file url django admin in docker compose HOT 19
- Storage does not follow Django docs recommendations HOT 14
- initialize_buckets command is stuck for several hours HOT 1
- Why is it necessary to have both public and private buckets? HOT 1
- Inconsistent use of `MINIO_USE_HTTPS` HOT 1
- TypeError: can't compare offset-naive and offset-aware datetimes when running collectstatic HOT 3
- Minio 7.0.2 causes error messages in installation HOT 1
- Getting ConnectionError in tox environment from call to check_bucket_existence when using static files storage HOT 2
- Exception ignored in: <function Minio.__del__ at 0x7f5cb166feb0> HOT 2
- Download url not working HOT 2
- change bucket_name in model HOT 1
- external endpoint are broken HOT 5
- How to have multiple MINIO_MEDIA_FILES_BUCKET? HOT 1
- ImportError: cannot import name 'utc' from 'django.utils.timezone' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-minio-backend.