We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Import not working on Kubernetes deployment
The file upload for import is not working on Kubernetes deployment. (but works locally and on heroku)



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `app/helpers/storage.py`
Content:
```
1 import os
2 from base64 import b64encode
3 from shutil import copyfile, rmtree
4 
5 from boto.gs.connection import GSConnection
6 from flask.ext.scrypt import generate_password_hash
7 from boto.s3.connection import S3Connection
8 from boto.s3.key import Key
9 from werkzeug.utils import secure_filename
10 import magic
11 
12 from app.settings import get_settings
13 
14 #################
15 # STORAGE SCHEMA
16 #################
17 
18 UPLOAD_PATHS = {
19     'sessions': {
20         'video': 'events/{event_id}/sessions/{id}/video',
21         'audio': 'events/{event_id}/audios/{id}/audio',
22         'slides': 'events/{event_id}/slides/{id}/slides'
23     },
24     'speakers': {
25         'photo': 'events/{event_id}/speakers/{id}/photo',
26         'thumbnail': 'events/{event_id}/speakers/{id}/thumbnail',
27         'small': 'events/{event_id}/speakers/{id}/small',
28         'icon': 'events/{event_id}/speakers/{id}/icon'
29     },
30     'event': {
31         'logo': 'events/{event_id}/logo',
32         'background_url': 'events/{event_id}/background',
33         'thumbnail': 'events/{event_id}/thumbnail',
34         'large': 'events/{event_id}/large',
35         'icon': 'events/{event_id}/icon'
36     },
37     'sponsors': {
38         'logo': 'events/{event_id}/sponsors/{id}/logo'
39     },
40     'tracks': {
41         'track_image_url': 'events/{event_id}/tracks/{id}/track_image'
42     },
43     'user': {
44         'avatar': 'users/{user_id}/avatar',
45         'thumbnail': 'users/{user_id}/thumbnail',
46         'small': 'users/{user_id}/small',
47         'icon': 'users/{user_id}/icon'
48     },
49     'temp': {
50         'event': 'events/temp/{uuid}'
51     }
52 }
53 
54 
55 ################
56 # HELPER CLASSES
57 ################
58 
59 class UploadedFile(object):
60     """
61     Helper for a disk-file to replicate request.files[ITEM] class
62     """
63     def __init__(self, file_path, filename):
64         self.file_path = file_path
65         self.filename = filename
66         self.file = open(file_path)
67 
68     def save(self, new_path):
69         copyfile(self.file_path, new_path)
70 
71     def read(self):
72         return self.file.read()
73 
74     def __exit__(self, *args, **kwargs):
75         self.file.close()
76 
77 
78 class UploadedMemory(object):
79     """
80     Helper for a memory file to replicate request.files[ITEM] class
81     """
82     def __init__(self, data, filename):
83         self.data = data
84         self.filename = filename
85 
86     def read(self):
87         return self.data
88 
89     def save(self, path):
90         f = open(path, 'w')
91         f.write(self.data)
92         f.close()
93 
94 
95 #########
96 # MAIN
97 #########
98 
99 def upload(uploaded_file, key, **kwargs):
100     """
101     Upload handler
102     """
103     # refresh settings
104     aws_bucket_name = get_settings()['aws_bucket_name']
105     aws_key = get_settings()['aws_key']
106     aws_secret = get_settings()['aws_secret']
107 
108     gs_bucket_name = get_settings()['gs_bucket_name']
109     gs_key = get_settings()['gs_key']
110     gs_secret = get_settings()['gs_secret']
111 
112     storage_place = get_settings()['storage_place']
113 
114     # upload
115     if aws_bucket_name and aws_key and aws_secret and storage_place == 's3':
116         return upload_to_aws(aws_bucket_name, aws_key, aws_secret, uploaded_file, key, **kwargs)
117     elif gs_bucket_name and gs_key and gs_secret and storage_place == 'gs':
118         return upload_to_gs(gs_bucket_name, gs_key, gs_secret, uploaded_file, key, **kwargs)
119     else:
120         return upload_local(uploaded_file, key, **kwargs)
121 
122 
123 def upload_local(uploaded_file, key, **kwargs):
124     """
125     Uploads file locally. Base dir - static/media/
126     """
127     filename = secure_filename(uploaded_file.filename)
128     file_path = 'static/media/' + key + '/' + generate_hash(key) + '/' + filename
129     dir_path = file_path.rsplit('/', 1)[0]
130     # delete current
131     try:
132         rmtree(dir_path)
133     except OSError:
134         pass
135     # create dirs
136     if not os.path.isdir(dir_path):
137         os.makedirs(dir_path)
138     uploaded_file.save(file_path)
139     return '/serve_' + file_path
140 
141 
142 def upload_to_aws(bucket_name, aws_key, aws_secret, file, key, acl='public-read'):
143     """
144     Uploads to AWS at key
145     http://{bucket}.s3.amazonaws.com/{key}
146     """
147     conn = S3Connection(aws_key, aws_secret)
148     bucket = conn.get_bucket(bucket_name)
149     k = Key(bucket)
150     # generate key
151     filename = secure_filename(file.filename)
152     key_dir = key + '/' + generate_hash(key) + '/'
153     k.key = key_dir + filename
154     # delete old data
155     for item in bucket.list(prefix='/' + key_dir):
156         item.delete()
157     # set object settings
158 
159     file_data = file.read()
160     file_mime = magic.from_buffer(file_data, mime=True)
161     size = len(file_data)
162     sent = k.set_contents_from_string(
163         file_data,
164         headers={
165             'Content-Disposition': 'attachment; filename=%s' % filename,
166             'Content-Type': '%s' % file_mime
167         }
168     )
169     k.set_acl(acl)
170     s3_url = 'https://%s.s3.amazonaws.com/' % bucket_name
171     if sent == size:
172         return s3_url + k.key
173     return False
174 
175 
176 def upload_to_gs(bucket_name, client_id, client_secret, file, key, acl='public-read'):
177     conn = GSConnection(client_id, client_secret)
178     bucket = conn.get_bucket(bucket_name)
179     k = Key(bucket)
180     # generate key
181     filename = secure_filename(file.filename)
182     key_dir = key + '/' + generate_hash(key) + '/'
183     k.key = key_dir + filename
184     # delete old data
185     for item in bucket.list(prefix='/' + key_dir):
186         item.delete()
187     # set object settings
188 
189     file_data = file.read()
190     file_mime = magic.from_buffer(file_data, mime=True)
191     size = len(file_data)
192     sent = k.set_contents_from_string(
193         file_data,
194         headers={
195             'Content-Disposition': 'attachment; filename=%s' % filename,
196             'Content-Type': '%s' % file_mime
197         }
198     )
199     k.set_acl(acl)
200     gs_url = 'https://storage.googleapis.com/%s/' % bucket_name
201     if sent == size:
202         return gs_url + key
203     return False
204 
205 # ########
206 # HELPERS
207 # ########
208 
209 
210 def generate_hash(key):
211     """
212     Generate hash for key
213     """
214     phash = generate_password_hash(key, get_settings()['secret'])
215     return b64encode(phash)[:10]  # limit len to 10, is sufficient
216
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/app/helpers/storage.py b/app/helpers/storage.py
--- a/app/helpers/storage.py
+++ b/app/helpers/storage.py
@@ -199,7 +199,7 @@
     k.set_acl(acl)
     gs_url = 'https://storage.googleapis.com/%s/' % bucket_name
     if sent == size:
-        return gs_url + key
+        return gs_url + k.key
     return False
 
 # ########

{"golden_diff": "diff --git a/app/helpers/storage.py b/app/helpers/storage.py\n--- a/app/helpers/storage.py\n+++ b/app/helpers/storage.py\n@@ -199,7 +199,7 @@\n     k.set_acl(acl)\n     gs_url = 'https://storage.googleapis.com/%s/' % bucket_name\n     if sent == size:\n-        return gs_url + key\n+        return gs_url + k.key\n     return False\n \n # ########\n", "issue": "Import not working on Kubernetes deployment\nThe file upload for import is not working on Kubernetes deployment. (but works locally and on heroku)\r\n\r\n\n", "before_files": [{"content": "import os\nfrom base64 import b64encode\nfrom shutil import copyfile, rmtree\n\nfrom boto.gs.connection import GSConnection\nfrom flask.ext.scrypt import generate_password_hash\nfrom boto.s3.connection import S3Connection\nfrom boto.s3.key import Key\nfrom werkzeug.utils import secure_filename\nimport magic\n\nfrom app.settings import get_settings\n\n#################\n# STORAGE SCHEMA\n#################\n\nUPLOAD_PATHS = {\n    'sessions': {\n        'video': 'events/{event_id}/sessions/{id}/video',\n        'audio': 'events/{event_id}/audios/{id}/audio',\n        'slides': 'events/{event_id}/slides/{id}/slides'\n    },\n    'speakers': {\n        'photo': 'events/{event_id}/speakers/{id}/photo',\n        'thumbnail': 'events/{event_id}/speakers/{id}/thumbnail',\n        'small': 'events/{event_id}/speakers/{id}/small',\n        'icon': 'events/{event_id}/speakers/{id}/icon'\n    },\n    'event': {\n        'logo': 'events/{event_id}/logo',\n        'background_url': 'events/{event_id}/background',\n        'thumbnail': 'events/{event_id}/thumbnail',\n        'large': 'events/{event_id}/large',\n        'icon': 'events/{event_id}/icon'\n    },\n    'sponsors': {\n        'logo': 'events/{event_id}/sponsors/{id}/logo'\n    },\n    'tracks': {\n        'track_image_url': 'events/{event_id}/tracks/{id}/track_image'\n    },\n    'user': {\n        'avatar': 'users/{user_id}/avatar',\n        'thumbnail': 'users/{user_id}/thumbnail',\n        'small': 'users/{user_id}/small',\n        'icon': 'users/{user_id}/icon'\n    },\n    'temp': {\n        'event': 'events/temp/{uuid}'\n    }\n}\n\n\n################\n# HELPER CLASSES\n################\n\nclass UploadedFile(object):\n    \"\"\"\n    Helper for a disk-file to replicate request.files[ITEM] class\n    \"\"\"\n    def __init__(self, file_path, filename):\n        self.file_path = file_path\n        self.filename = filename\n        self.file = open(file_path)\n\n    def save(self, new_path):\n        copyfile(self.file_path, new_path)\n\n    def read(self):\n        return self.file.read()\n\n    def __exit__(self, *args, **kwargs):\n        self.file.close()\n\n\nclass UploadedMemory(object):\n    \"\"\"\n    Helper for a memory file to replicate request.files[ITEM] class\n    \"\"\"\n    def __init__(self, data, filename):\n        self.data = data\n        self.filename = filename\n\n    def read(self):\n        return self.data\n\n    def save(self, path):\n        f = open(path, 'w')\n        f.write(self.data)\n        f.close()\n\n\n#########\n# MAIN\n#########\n\ndef upload(uploaded_file, key, **kwargs):\n    \"\"\"\n    Upload handler\n    \"\"\"\n    # refresh settings\n    aws_bucket_name = get_settings()['aws_bucket_name']\n    aws_key = get_settings()['aws_key']\n    aws_secret = get_settings()['aws_secret']\n\n    gs_bucket_name = get_settings()['gs_bucket_name']\n    gs_key = get_settings()['gs_key']\n    gs_secret = get_settings()['gs_secret']\n\n    storage_place = get_settings()['storage_place']\n\n    # upload\n    if aws_bucket_name and aws_key and aws_secret and storage_place == 's3':\n        return upload_to_aws(aws_bucket_name, aws_key, aws_secret, uploaded_file, key, **kwargs)\n    elif gs_bucket_name and gs_key and gs_secret and storage_place == 'gs':\n        return upload_to_gs(gs_bucket_name, gs_key, gs_secret, uploaded_file, key, **kwargs)\n    else:\n        return upload_local(uploaded_file, key, **kwargs)\n\n\ndef upload_local(uploaded_file, key, **kwargs):\n    \"\"\"\n    Uploads file locally. Base dir - static/media/\n    \"\"\"\n    filename = secure_filename(uploaded_file.filename)\n    file_path = 'static/media/' + key + '/' + generate_hash(key) + '/' + filename\n    dir_path = file_path.rsplit('/', 1)[0]\n    # delete current\n    try:\n        rmtree(dir_path)\n    except OSError:\n        pass\n    # create dirs\n    if not os.path.isdir(dir_path):\n        os.makedirs(dir_path)\n    uploaded_file.save(file_path)\n    return '/serve_' + file_path\n\n\ndef upload_to_aws(bucket_name, aws_key, aws_secret, file, key, acl='public-read'):\n    \"\"\"\n    Uploads to AWS at key\n    http://{bucket}.s3.amazonaws.com/{key}\n    \"\"\"\n    conn = S3Connection(aws_key, aws_secret)\n    bucket = conn.get_bucket(bucket_name)\n    k = Key(bucket)\n    # generate key\n    filename = secure_filename(file.filename)\n    key_dir = key + '/' + generate_hash(key) + '/'\n    k.key = key_dir + filename\n    # delete old data\n    for item in bucket.list(prefix='/' + key_dir):\n        item.delete()\n    # set object settings\n\n    file_data = file.read()\n    file_mime = magic.from_buffer(file_data, mime=True)\n    size = len(file_data)\n    sent = k.set_contents_from_string(\n        file_data,\n        headers={\n            'Content-Disposition': 'attachment; filename=%s' % filename,\n            'Content-Type': '%s' % file_mime\n        }\n    )\n    k.set_acl(acl)\n    s3_url = 'https://%s.s3.amazonaws.com/' % bucket_name\n    if sent == size:\n        return s3_url + k.key\n    return False\n\n\ndef upload_to_gs(bucket_name, client_id, client_secret, file, key, acl='public-read'):\n    conn = GSConnection(client_id, client_secret)\n    bucket = conn.get_bucket(bucket_name)\n    k = Key(bucket)\n    # generate key\n    filename = secure_filename(file.filename)\n    key_dir = key + '/' + generate_hash(key) + '/'\n    k.key = key_dir + filename\n    # delete old data\n    for item in bucket.list(prefix='/' + key_dir):\n        item.delete()\n    # set object settings\n\n    file_data = file.read()\n    file_mime = magic.from_buffer(file_data, mime=True)\n    size = len(file_data)\n    sent = k.set_contents_from_string(\n        file_data,\n        headers={\n            'Content-Disposition': 'attachment; filename=%s' % filename,\n            'Content-Type': '%s' % file_mime\n        }\n    )\n    k.set_acl(acl)\n    gs_url = 'https://storage.googleapis.com/%s/' % bucket_name\n    if sent == size:\n        return gs_url + key\n    return False\n\n# ########\n# HELPERS\n# ########\n\n\ndef generate_hash(key):\n    \"\"\"\n    Generate hash for key\n    \"\"\"\n    phash = generate_password_hash(key, get_settings()['secret'])\n    return b64encode(phash)[:10]  # limit len to 10, is sufficient\n", "path": "app/helpers/storage.py"}], "after_files": [{"content": "import os\nfrom base64 import b64encode\nfrom shutil import copyfile, rmtree\n\nfrom boto.gs.connection import GSConnection\nfrom flask.ext.scrypt import generate_password_hash\nfrom boto.s3.connection import S3Connection\nfrom boto.s3.key import Key\nfrom werkzeug.utils import secure_filename\nimport magic\n\nfrom app.settings import get_settings\n\n#################\n# STORAGE SCHEMA\n#################\n\nUPLOAD_PATHS = {\n    'sessions': {\n        'video': 'events/{event_id}/sessions/{id}/video',\n        'audio': 'events/{event_id}/audios/{id}/audio',\n        'slides': 'events/{event_id}/slides/{id}/slides'\n    },\n    'speakers': {\n        'photo': 'events/{event_id}/speakers/{id}/photo',\n        'thumbnail': 'events/{event_id}/speakers/{id}/thumbnail',\n        'small': 'events/{event_id}/speakers/{id}/small',\n        'icon': 'events/{event_id}/speakers/{id}/icon'\n    },\n    'event': {\n        'logo': 'events/{event_id}/logo',\n        'background_url': 'events/{event_id}/background',\n        'thumbnail': 'events/{event_id}/thumbnail',\n        'large': 'events/{event_id}/large',\n        'icon': 'events/{event_id}/icon'\n    },\n    'sponsors': {\n        'logo': 'events/{event_id}/sponsors/{id}/logo'\n    },\n    'tracks': {\n        'track_image_url': 'events/{event_id}/tracks/{id}/track_image'\n    },\n    'user': {\n        'avatar': 'users/{user_id}/avatar',\n        'thumbnail': 'users/{user_id}/thumbnail',\n        'small': 'users/{user_id}/small',\n        'icon': 'users/{user_id}/icon'\n    },\n    'temp': {\n        'event': 'events/temp/{uuid}'\n    }\n}\n\n\n################\n# HELPER CLASSES\n################\n\nclass UploadedFile(object):\n    \"\"\"\n    Helper for a disk-file to replicate request.files[ITEM] class\n    \"\"\"\n    def __init__(self, file_path, filename):\n        self.file_path = file_path\n        self.filename = filename\n        self.file = open(file_path)\n\n    def save(self, new_path):\n        copyfile(self.file_path, new_path)\n\n    def read(self):\n        return self.file.read()\n\n    def __exit__(self, *args, **kwargs):\n        self.file.close()\n\n\nclass UploadedMemory(object):\n    \"\"\"\n    Helper for a memory file to replicate request.files[ITEM] class\n    \"\"\"\n    def __init__(self, data, filename):\n        self.data = data\n        self.filename = filename\n\n    def read(self):\n        return self.data\n\n    def save(self, path):\n        f = open(path, 'w')\n        f.write(self.data)\n        f.close()\n\n\n#########\n# MAIN\n#########\n\ndef upload(uploaded_file, key, **kwargs):\n    \"\"\"\n    Upload handler\n    \"\"\"\n    # refresh settings\n    aws_bucket_name = get_settings()['aws_bucket_name']\n    aws_key = get_settings()['aws_key']\n    aws_secret = get_settings()['aws_secret']\n\n    gs_bucket_name = get_settings()['gs_bucket_name']\n    gs_key = get_settings()['gs_key']\n    gs_secret = get_settings()['gs_secret']\n\n    storage_place = get_settings()['storage_place']\n\n    # upload\n    if aws_bucket_name and aws_key and aws_secret and storage_place == 's3':\n        return upload_to_aws(aws_bucket_name, aws_key, aws_secret, uploaded_file, key, **kwargs)\n    elif gs_bucket_name and gs_key and gs_secret and storage_place == 'gs':\n        return upload_to_gs(gs_bucket_name, gs_key, gs_secret, uploaded_file, key, **kwargs)\n    else:\n        return upload_local(uploaded_file, key, **kwargs)\n\n\ndef upload_local(uploaded_file, key, **kwargs):\n    \"\"\"\n    Uploads file locally. Base dir - static/media/\n    \"\"\"\n    filename = secure_filename(uploaded_file.filename)\n    file_path = 'static/media/' + key + '/' + generate_hash(key) + '/' + filename\n    dir_path = file_path.rsplit('/', 1)[0]\n    # delete current\n    try:\n        rmtree(dir_path)\n    except OSError:\n        pass\n    # create dirs\n    if not os.path.isdir(dir_path):\n        os.makedirs(dir_path)\n    uploaded_file.save(file_path)\n    return '/serve_' + file_path\n\n\ndef upload_to_aws(bucket_name, aws_key, aws_secret, file, key, acl='public-read'):\n    \"\"\"\n    Uploads to AWS at key\n    http://{bucket}.s3.amazonaws.com/{key}\n    \"\"\"\n    conn = S3Connection(aws_key, aws_secret)\n    bucket = conn.get_bucket(bucket_name)\n    k = Key(bucket)\n    # generate key\n    filename = secure_filename(file.filename)\n    key_dir = key + '/' + generate_hash(key) + '/'\n    k.key = key_dir + filename\n    # delete old data\n    for item in bucket.list(prefix='/' + key_dir):\n        item.delete()\n    # set object settings\n\n    file_data = file.read()\n    file_mime = magic.from_buffer(file_data, mime=True)\n    size = len(file_data)\n    sent = k.set_contents_from_string(\n        file_data,\n        headers={\n            'Content-Disposition': 'attachment; filename=%s' % filename,\n            'Content-Type': '%s' % file_mime\n        }\n    )\n    k.set_acl(acl)\n    s3_url = 'https://%s.s3.amazonaws.com/' % bucket_name\n    if sent == size:\n        return s3_url + k.key\n    return False\n\n\ndef upload_to_gs(bucket_name, client_id, client_secret, file, key, acl='public-read'):\n    conn = GSConnection(client_id, client_secret)\n    bucket = conn.get_bucket(bucket_name)\n    k = Key(bucket)\n    # generate key\n    filename = secure_filename(file.filename)\n    key_dir = key + '/' + generate_hash(key) + '/'\n    k.key = key_dir + filename\n    # delete old data\n    for item in bucket.list(prefix='/' + key_dir):\n        item.delete()\n    # set object settings\n\n    file_data = file.read()\n    file_mime = magic.from_buffer(file_data, mime=True)\n    size = len(file_data)\n    sent = k.set_contents_from_string(\n        file_data,\n        headers={\n            'Content-Disposition': 'attachment; filename=%s' % filename,\n            'Content-Type': '%s' % file_mime\n        }\n    )\n    k.set_acl(acl)\n    gs_url = 'https://storage.googleapis.com/%s/' % bucket_name\n    if sent == size:\n        return gs_url + k.key\n    return False\n\n# ########\n# HELPERS\n# ########\n\n\ndef generate_hash(key):\n    \"\"\"\n    Generate hash for key\n    \"\"\"\n    phash = generate_password_hash(key, get_settings()['secret'])\n    return b64encode(phash)[:10]  # limit len to 10, is sufficient\n", "path": "app/helpers/storage.py"}]}