I cant get the full path of the client side to upload to AWS S3
Ajax:
upload_file(start, path) {
var end;
var self = this;
var existingPath = path;
var formData = new FormData();
var nextChunk = start + this.max_length + 1;
var currentChunk = this.file.slice(start, nextChunk);
var uploadedChunk = start + currentChunk.size
if (uploadedChunk >= this.file.size) {
end = 1;
} else {
end = 0;
}
formData.append('file', currentChunk);
formData.append('filename', this.file.name);
formData.append('end', end);
formData.append('existingPath', existingPath);
formData.append('nextSlice', nextChunk);
$('.filename').text(this.file.name)
$('.textbox').text("Uploading file")
$.ajaxSetup({
// make sure to send the header
headers: {
"X-CSRFToken": document.querySelector('[name=csrfmiddlewaretoken]').value,
}
});
$.ajax({
xhr: function () {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function (e) {
if (e.lengthComputable) {
if (self.file.size < self.max_length) {
var percent = Math.round((e.loaded / e.total) * 100);
} else {
var percent = Math.round((uploadedChunk / self.file.size) * 100);
}
$('.progress-bar').css('width', percent + '%')
$('.progress-bar').text(percent + '%')
}
});
return xhr;
},
url: '/fileUploader/',
type: 'POST',
dataType: 'json',
cache: false,
processData: false,
contentType: false,
data: formData,
error: function (xhr) {
alert(xhr.statusText);
},
success: function (res) {
if (nextChunk < self.file.size) {
// upload file in chunks
existingPath = res.existingPath
self.upload_file(nextChunk, existingPath);
} else {
// upload complete
$('.textbox').text(res.data);
alert(res.data)
}
}
})
and my views.py
def create_post(request):
# s3_client=boto3.client('s3')
s3 = boto3.client('s3', region_name=settings.AWS_S3_REGION_NAME, aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
categories_1 = Category.objects.all()
if request.method == 'POST':
file = request.FILES['file'].read()
file_name_s3 = request.POST['filename']
fileName= request.POST['filename'].replace(' ', '')
fileName_STR = re.sub("[^A-Z]", "", fileName,0,re.IGNORECASE) + '.mp4'
existingPath = request.POST['existingPath']
end = request.POST['end']
image = request.FILES.get('image', False)
imageName = request.FILES.get('imagename', False)
if image != False:
image = request.FILES['image'].read()
imageName= request.POST['imagename'].replace(' ', '')
else:
pass
title = request.POST['title']
tags = request.POST['tags']
categories = parse_ids(request.POST['categories'])
description = request.POST['description']
nextSlice = request.POST['nextSlice']
if file=="" or fileName_STR=="" or existingPath=="" or end=="" or nextSlice=="":
res = JsonResponse({'data':'Invalid Request'})
return res
else:
if existingPath == 'null':
path = 'media/uploads/video_files/' + fileName
if image != False:
with open(path, 'wb+') as destination:
destination.write(file)
imagePath = 'media/thumbnail/' + imageName
else:
pass
if image:
with open(imagePath, 'wb+') as destination:
destination.write(image)
else:
pass
path = 'media/uploads/video_files/' + fileName_STR
# THIS IS THE PROBLEM...................
s3.upload_file(file_name_s3 ,bucket,str(fileName_STR))
# ............................................
FileFolder = Post()
FileFolder.video = 'uploads/video_files/'+fileName_STR
FileFolder.existingPath = fileName_STR
FileFolder.eof = end
FileFolder.title = title
FileFolder.author = request.user
FileFolder.description = description
if image == False:
FileFolder.thumbnail = None
else:
FileFolder.thumbnail = 'thumbnail/' + imageName
FileFolder.approved = True
FileFolder.save()
tag_list = taggit.utils._parse_tags(tags)
FileFolder.tags.add(*tag_list)
categories_post = Category.objects.filter(id__in=categories)
if categories_post:
for category in categories_post:
FileFolder.categories.add(category)
if int(end):
res = JsonResponse({'data':'Uploaded Successfully','existingPath': fileName})
else:
res = JsonResponse({'existingPath': fileName_STR})
return res
else:
path = 'media/uploads/video_files/' + existingPath
model_id = Post.objects.get(existingPath=existingPath)
if model_id.title == title:
if not model_id.eof:
with open(path, 'ab+') as destination:
destination.write(file)
if int(end):
model_id.eof = int(end)
model_id.save()
res = JsonResponse({'data':'Uploaded Successfully','existingPath':model_id.existingPath})
else:
res = JsonResponse({'existingPath':model_id.existingPath})
return res
else:
res = JsonResponse({'data':'EOF found. Invalid request'})
return res
else:
res = JsonResponse({'data':'No such file exists in the existingPath'})
return res
return render(request, 'main/create_post.html', {'categories': categories_1})
HOW TO GET THE PATH OF THE FILE SO I CAN UPLOAD TO AWS S3, no matter what i tried it doesnt gives me the full path
FileNotFoundError: [Errno 2] No such file or directory: ‘Bones – TheArtOfCremation-tqSpI1Jm0YE-1080p.mp4’