Ubuntu

사용자별 버킷 생성

...@@ -59,6 +59,7 @@ def logout(request): ...@@ -59,6 +59,7 @@ def logout(request):
59 59
60 def register(request): 60 def register(request):
61 Cog = cognito.Cognito() 61 Cog = cognito.Cognito()
62 + reg=views.FileView()
62 if request.user.is_authenticated: raise PermissionDenied 63 if request.user.is_authenticated: raise PermissionDenied
63 if request.method == "POST": 64 if request.method == "POST":
64 require_keys = ('username', 'password', 'first_name', 'last_name', 'email') 65 require_keys = ('username', 'password', 'first_name', 'last_name', 'email')
...@@ -97,7 +98,7 @@ def register(request): ...@@ -97,7 +98,7 @@ def register(request):
97 }, 98 },
98 ]) 99 ])
99 Cog.confirm_sign_up(username=request.POST['username']); 100 Cog.confirm_sign_up(username=request.POST['username']);
100 - print(views.create_bucket) 101 + print(reg.create_bucket(request))
101 return redirect('/main') 102 return redirect('/main')
102 else: 103 else:
103 return render(request, 'register.html', { 104 return render(request, 'register.html', {
......
...@@ -25,10 +25,6 @@ config.read('config.ini') ...@@ -25,10 +25,6 @@ config.read('config.ini')
25 # file.name 25 # file.name
26 # ) 26 # )
27 # file_urls = [f"https://s3.us-ease-1.amazonaws.com/khuloud/{file.name}" for file in request.FILES.getlist('file')] 27 # file_urls = [f"https://s3.us-ease-1.amazonaws.com/khuloud/{file.name}" for file in request.FILES.getlist('file')]
28 -
29 -# return JsonResponse({'files':file_urls}, status=200)
30 -
31 -
32 class FileView(View): 28 class FileView(View):
33 #keys=aws_key() 29 #keys=aws_key()
34 s3_client = boto3.client( 30 s3_client = boto3.client(
...@@ -36,8 +32,6 @@ class FileView(View): ...@@ -36,8 +32,6 @@ class FileView(View):
36 aws_access_key_id = config['aws']['AWS_ACCESS_KEY_ID'], 32 aws_access_key_id = config['aws']['AWS_ACCESS_KEY_ID'],
37 aws_secret_access_key=config['aws']['AWS_SECRET_ACCESS_KEY'] 33 aws_secret_access_key=config['aws']['AWS_SECRET_ACCESS_KEY']
38 ) 34 )
39 -
40 -
41 @csrf_exempt 35 @csrf_exempt
42 def post(self, request): 36 def post(self, request):
43 # filename = request.data.get('filename') 37 # filename = request.data.get('filename')
...@@ -46,9 +40,17 @@ class FileView(View): ...@@ -46,9 +40,17 @@ class FileView(View):
46 self.s3_client.upload_file(filepath, bucket_name, filepath) 40 self.s3_client.upload_file(filepath, bucket_name, filepath)
47 s3link='https://s3.console.aws.amazon.com/s3/buckets/'+bucket_name+'/'+filepath 41 s3link='https://s3.console.aws.amazon.com/s3/buckets/'+bucket_name+'/'+filepath
48 return JsonResponse({'file':s3link}) 42 return JsonResponse({'file':s3link})
49 - 43 + @csrf_exempt
50 -@csrf_exempt 44 + def create_bucket(self,request):
51 -def create_bucket(request): 45 + """
52 - s3 = boto3.client('s3') 46 + s3 = boto3.resource('s3',
53 - s3.create_bucket(Bucket='request.user.username') 47 + aws_access_key_id = config['aws']['AWS_ACCESS_KEY_ID'],
54 - return Bucket 48 + aws_secret_access_key=config['aws']['AWS_SECRET_ACCESS_KEY']
49 + )
50 + """
51 + bucket=self.s3_client.create_bucket(
52 + ACL='public-read-write',
53 + Bucket=request.POST['username'],
54 + CreateBucketConfiguration={
55 + 'LocationConstraint':'ap-northeast-2'},
56 + )
......