def get_file(url): """ sftp://Some_Compute_UUID/relative/path/from/base_path/file.fastq.gz :param url: :type url: :return: :rtype: """ url = urlparse(url) scheme = url.scheme compute_id = url.host compute = ComputeResource.objects.get(id=compute_id) host = compute.hostname port = compute.port if port is None: port = 22 private_key = compute.private_key username = compute.extra.get('username') base_dir = compute.extra.get('base_dir') params = dict(port=port, username=username, pkey=RSAKey.from_private_key(StringIO(private_key))) storage = SFTPStorage(host=host, params=params) return storage.open(path.join(base_dir, url.path))
class TransporterSFTP(Transporter): name = "SFTP" valid_settings = ImmutableSet(["host", "username", "password", "url", "port", "path", "key"]) required_settings = ImmutableSet(["host", "username", "url"]) def __init__(self, settings, callback, error_callback, parent_logger=None): Transporter.__init__(self, settings, callback, error_callback, parent_logger) # Fill out defaults if necessary. configured_settings = Set(self.settings.keys()) if not "port" in configured_settings: self.settings["port"] = 22 if not "path" in configured_settings: self.settings["path"] = "" # Map the settings to the format expected by FTPStorage. if "password" in configured_settings: location = ( "sftp://" + self.settings["username"] + ":" + self.settings["password"] + "@" + self.settings["host"] + ":" + str(self.settings["port"]) + self.settings["path"] ) else: location = ( "sftp://" + self.settings["username"] + "@" + self.settings["host"] + ":" + str(self.settings["port"]) + self.settings["path"] ) key = None if "key" in configured_settings: key = self.settings["key"] self.storage = SFTPStorage(location, self.settings["url"], key) self.storage._start_connection() try: self.storage._start_connection() except Exception, e: raise ConnectionError(e)
from django.db import models from storages.backends.sftpstorage import SFTPStorage from tinymce.models import HTMLField sfs = SFTPStorage() class News(models.Model): title = models.CharField(max_length=100, verbose_name='Название') text = HTMLField(verbose_name='Текст') publication_date = models.DateField(verbose_name='Дата публикации') created_at = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='Опубликовано') def __str__(self): return self.title class Meta: verbose_name = 'Новость' verbose_name_plural = 'Новости' ordering = [ '-created_at', ] class NewsImage(models.Model): news = models.ForeignKey(News, on_delete=models.CASCADE, verbose_name='Новость') file = models.FileField(blank=True,
from django.db import models from storages.backends.sftpstorage import SFTPStorage SFS = SFTPStorage() # Create your models here. class ImageModel(models.Model): name = models.CharField(max_length=50) hour_accum = models.IntegerField() file = models.ImageField(upload_to='images', max_length=None) def __str__(self): return self.name
from .serializers import ( AgencyGeneralDocumentsSerializer, AgencySecureDocumentSerializer, CandidateGeneralDocumentsSerializer, CandidateSecureDocumentSerializer, CompanyGeneralDocumentsSerializer, CompanySecureDocumentSerializer, RetrieveUpdateCandidateGeneralDocumentsSerializer, RetrieveUpdateCompanyGeneralDocumentsSerializer, UploadAgencySecureDocumentSerializer, UploadCandidateSecureDocumentSerializer, UploadCompanySecureDocumentSerializer, ) from .validators import ChangeFieldValidator STORAGE = SFTPStorage() class UploadCompanyGeneralDocumentsView(generics.CreateAPIView): """ Endpoint for Company to upload general documents """ serializer_class = CompanyGeneralDocumentsSerializer permission_classes = (IsAuthenticated, IsCompany, IsOwner) def post(self, request, *args, **kwargs): user_id = request.user.id if CompanyGeneralDocuments.objects.filter(user_id=user_id).exists(): return Response( { 'status':