David Larlet avatar David Larlet committed 113abef

S3 & Boto: clean paths' names for windows, refs #11. Thanks Jason Christa.

Comments (0)

Files changed (2)

     def _get_connection(self):
         return AWSAuthConnection(*self._get_access_keys())
 
+    def _clean_name(self, name):
+        # Useful for windows' paths
+        return os.path.normpath(name).replace('\\', '/')
+
     def _put_file(self, name, content):
         content_type = mimetypes.guess_type(name)[0] or "application/x-octet-stream"
         self.headers.update({'x-amz-acl': self.acl, 'Content-Type': content_type})
             raise IOError("S3StorageError: %s" % response.message)
 
     def _open(self, name, mode='rb'):
+        name = self._clean_name(name)
         remote_file = S3StorageFile(name, self, mode=mode)
         return remote_file
 
     def _read(self, name, start_range=None, end_range=None):
+        name = self._clean_name(name)
         if start_range is None:
             headers = {}
         else:
         return response.object.data, headers.get('etag', None), headers.get('content-range', None)
         
     def _save(self, name, content):
+        name = self._clean_name(name)
         content.open()
         if hasattr(content, 'chunks'):
             content_str = ''.join(chunk for chunk in content.chunks())
         return name
     
     def delete(self, name):
+        name = self._clean_name(name)
         response = self.connection.delete(self.bucket, name)
         if response.http_response.status != 204:
             raise IOError("S3StorageError: %s" % response.message)
 
     def exists(self, name):
+        name = self._clean_name(name)
         response = self.connection._make_request('HEAD', self.bucket, name)
         return response.status == 200
 
     def size(self, name):
+        name = self._clean_name(name)
         response = self.connection._make_request('HEAD', self.bucket, name)
         content_length = response.getheader('Content-Length')
         return content_length and int(content_length) or 0
     
     def url(self, name):
+        name = self._clean_name(name)
         if QUERYSTRING_ACTIVE:
             return self.generator.generate_url('GET', self.bucket, name)
         else:
     ## UNCOMMENT BELOW IF NECESSARY
     #def get_available_name(self, name):
     #    """ Overwrite existing file with the same name. """
+    #    name = self._clean_name(name)
     #    return name
 
 

backends/s3boto.py

         
         return None, None
     
+    def _clean_name(self, name):
+        # Useful for windows' paths
+        return os.path.normpath(name).replace('\\', '/')
+
     def _open(self, name, mode='rb'):
+        name = self._clean_name(name)
         return S3BotoStorageFile(name, mode, self)
     
     def _save(self, name, content):
+        name = self._clean_name(name)
         headers = self.headers
         if hasattr(content.file, 'content_type'):
             headers['Content-Type'] = content.file.content_type
         return name
     
     def delete(self, name):
+        name = self._clean_name(name)
         self.bucket.delete_key(name)
     
     def exists(self, name):
+        name = self._clean_name(name)
         k = Key(self.bucket, name)
         return k.exists()
     
     def listdir(self, name):
+        name = self._clean_name(name)
         return [l.name for l in self.bucket.list() if not len(name) or l.name[:len(name)] == name]
     
     def size(self, name):
+        name = self._clean_name(name)
         return self.bucket.get_key(name).size
     
     def url(self, name):
+        name = self._clean_name(name)
         return self.bucket.get_key(name).generate_url(QUERYSTRING_EXPIRE, method='GET', query_auth=QUERYSTRING_AUTH)
     
     def get_available_name(self, name):
         """ Overwrite existing file with the same name. """
+        name = self._clean_name(name)
         return name
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.