コード例 #1
0
func main() {

	fmt.Println("hello there")

	// Create a connection
	c := swift.Connection{
		UserName:    "******",
		ApiKey:      "password",
		AuthUrl:     "auth-url",
		Domain:      "domain", // Name of the domain
		AuthVersion: 3,
		TenantId:    "tenantId", // Id of the tenant/project
	}

	// Authenticate
	err := c.Authenticate()
	if err != nil {
		panic(err)
	}

	// List all containers
	containers, err := c.ContainerNamesAll(nil)
	if err != nil {
		panic(err)
	}
	fmt.Println("Containers: ")
	fmt.Println(containers)

	// Create new container
	err1 := c.ContainerCreate("testContainer", nil)
	if err1 != nil {
		panic(err1)
	}
	fmt.Println("Container created")

	// List objects
	object, headers, err := c.Object(CONTAINER, OBJECT)
	if err != nil {
		panic(err)
	}
	fmt.Println("Object Info: ")
	fmt.Println(object.Name)
	fmt.Println(headers.ObjectMetadata())

	// Account info
	info, headers, err := c.Account()
	if err != nil {
		panic(err)
	}
	fmt.Println("Account Info: ")
	fmt.Println(info)
	fmt.Println(headers)

}
コード例 #2
0
ファイル: swift.go プロジェクト: vivekjuneja/distribution
// New constructs a new Driver with the given Openstack Swift credentials and container name
func New(params Parameters) (*Driver, error) {
	transport := &http.Transport{
		Proxy:               http.ProxyFromEnvironment,
		MaxIdleConnsPerHost: 2048,
		TLSClientConfig:     &tls.Config{InsecureSkipVerify: params.InsecureSkipVerify},
	}

	ct := swift.Connection{
		UserName:       params.Username,
		ApiKey:         params.Password,
		AuthUrl:        params.AuthURL,
		Region:         params.Region,
		UserAgent:      "distribution/" + version.Version,
		Tenant:         params.Tenant,
		TenantId:       params.TenantID,
		Domain:         params.Domain,
		DomainId:       params.DomainID,
		TrustId:        params.TrustID,
		Transport:      transport,
		ConnectTimeout: 60 * time.Second,
		Timeout:        15 * 60 * time.Second,
	}
	err := ct.Authenticate()
	if err != nil {
		return nil, fmt.Errorf("Swift authentication failed: %s", err)
	}

	if _, _, err := ct.Container(params.Container); err == swift.ContainerNotFound {
		if err := ct.ContainerCreate(params.Container, nil); err != nil {
			return nil, fmt.Errorf("Failed to create container %s (%s)", params.Container, err)
		}
	} else if err != nil {
		return nil, fmt.Errorf("Failed to retrieve info about container %s (%s)", params.Container, err)
	}

	d := &driver{
		Conn:              ct,
		Container:         params.Container,
		Prefix:            params.Prefix,
		BulkDeleteSupport: detectBulkDelete(params.AuthURL),
		ChunkSize:         params.ChunkSize,
	}

	return &Driver{
		baseEmbed: baseEmbed{
			Base: base.Base{
				StorageDriver: d,
			},
		},
	}, nil
}
コード例 #3
0
ファイル: swift_test.go プロジェクト: quixoten/vault
func TestSwiftBackend(t *testing.T) {
	if os.Getenv("OS_USERNAME") == "" || os.Getenv("OS_PASSWORD") == "" ||
		os.Getenv("OS_AUTH_URL") == "" {
		t.SkipNow()
	}
	username := os.Getenv("OS_USERNAME")
	password := os.Getenv("OS_PASSWORD")
	authUrl := os.Getenv("OS_AUTH_URL")
	tenant := os.Getenv("OS_TENANT_NAME")

	ts := time.Now().UnixNano()
	container := fmt.Sprintf("vault-test-%d", ts)

	cleaner := swift.Connection{
		UserName:  username,
		ApiKey:    password,
		AuthUrl:   authUrl,
		Tenant:    tenant,
		Transport: cleanhttp.DefaultPooledTransport(),
	}

	err := cleaner.Authenticate()
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	err = cleaner.ContainerCreate(container, nil)
	if nil != err {
		t.Fatalf("Unable to create test container '%s': %v", container, err)
	}
	defer func() {
		newObjects, err := cleaner.ObjectNamesAll(container, nil)
		if err != nil {
			t.Fatalf("err: %s", err)
		}
		for _, o := range newObjects {
			err := cleaner.ObjectDelete(container, o)
			if err != nil {
				t.Fatalf("err: %s", err)
			}
		}
		err = cleaner.ContainerDelete(container)
		if err != nil {
			t.Fatalf("err: %s", err)
		}
	}()

	logger := logformat.NewVaultLogger(log.LevelTrace)

	b, err := NewBackend("swift", logger, map[string]string{
		"username":  username,
		"password":  password,
		"container": container,
		"auth_url":  authUrl,
		"tenant":    tenant,
	})
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	testBackend(t, b)
	testBackend_ListPrefix(t, b)

}
コード例 #4
0
ファイル: swift.go プロジェクト: waynew/distribution
// New constructs a new Driver with the given Openstack Swift credentials and container name
func New(params Parameters) (*Driver, error) {
	transport := &http.Transport{
		Proxy:               http.ProxyFromEnvironment,
		MaxIdleConnsPerHost: 2048,
		TLSClientConfig:     &tls.Config{InsecureSkipVerify: params.InsecureSkipVerify},
	}

	ct := swift.Connection{
		UserName:       params.Username,
		ApiKey:         params.Password,
		AuthUrl:        params.AuthURL,
		Region:         params.Region,
		UserAgent:      "distribution/" + version.Version,
		Tenant:         params.Tenant,
		TenantId:       params.TenantID,
		Domain:         params.Domain,
		DomainId:       params.DomainID,
		TrustId:        params.TrustID,
		Transport:      transport,
		ConnectTimeout: 60 * time.Second,
		Timeout:        15 * 60 * time.Second,
	}
	err := ct.Authenticate()
	if err != nil {
		return nil, fmt.Errorf("Swift authentication failed: %s", err)
	}

	if _, _, err := ct.Container(params.Container); err == swift.ContainerNotFound {
		if err := ct.ContainerCreate(params.Container, nil); err != nil {
			return nil, fmt.Errorf("Failed to create container %s (%s)", params.Container, err)
		}
	} else if err != nil {
		return nil, fmt.Errorf("Failed to retrieve info about container %s (%s)", params.Container, err)
	}

	d := &driver{
		Conn:           ct,
		Container:      params.Container,
		Prefix:         params.Prefix,
		ChunkSize:      params.ChunkSize,
		TempURLMethods: make([]string, 0),
		AccessKey:      params.AccessKey,
	}

	info := swiftInfo{}
	if config, err := d.Conn.QueryInfo(); err == nil {
		_, d.BulkDeleteSupport = config["bulk_delete"]

		if err := mapstructure.Decode(config, &info); err == nil {
			d.TempURLContainerKey = info.Swift.Version >= "2.3.0"
			d.TempURLMethods = info.Tempurl.Methods
		}
	} else {
		d.TempURLContainerKey = params.TempURLContainerKey
		d.TempURLMethods = params.TempURLMethods
	}

	if len(d.TempURLMethods) > 0 {
		secretKey := params.SecretKey
		if secretKey == "" {
			secretKey, _ = generateSecret()
		}

		// Since Swift 2.2.2, we can now set secret keys on containers
		// in addition to the account secret keys. Use them in preference.
		if d.TempURLContainerKey {
			_, containerHeaders, err := d.Conn.Container(d.Container)
			if err != nil {
				return nil, fmt.Errorf("Failed to fetch container info %s (%s)", d.Container, err)
			}

			d.SecretKey = containerHeaders["X-Container-Meta-Temp-Url-Key"]
			if d.SecretKey == "" || (params.SecretKey != "" && d.SecretKey != params.SecretKey) {
				m := swift.Metadata{}
				m["temp-url-key"] = secretKey
				if d.Conn.ContainerUpdate(d.Container, m.ContainerHeaders()); err == nil {
					d.SecretKey = secretKey
				}
			}
		} else {
			// Use the account secret key
			_, accountHeaders, err := d.Conn.Account()
			if err != nil {
				return nil, fmt.Errorf("Failed to fetch account info (%s)", err)
			}

			d.SecretKey = accountHeaders["X-Account-Meta-Temp-Url-Key"]
			if d.SecretKey == "" || (params.SecretKey != "" && d.SecretKey != params.SecretKey) {
				m := swift.Metadata{}
				m["temp-url-key"] = secretKey
				if err := d.Conn.AccountUpdate(m.AccountHeaders()); err == nil {
					d.SecretKey = secretKey
				}
			}
		}
	}

	return &Driver{
		baseEmbed: baseEmbed{
			Base: base.Base{
				StorageDriver: d,
			},
		},
	}, nil
}
コード例 #5
0
ファイル: osync.go プロジェクト: jiangytcn/osync
func worker(c swift.Connection, parent string, level int) string {
	dirs, err := ioutil.ReadDir(parent)

	if nil != err {
		fmt.Println(err.Error())
	}

	var logstr string
	for _, f0 := range dirs {
		var curdir string
		if prefixlen == len(parent) {
			curdir = f0.Name()
		} else {
			curdir = parent[prefixlen:len(parent)]
			curdir = strings.TrimLeft(curdir, "/")
		}

		//		fmt.Println(parent, level, curdir)

		switch {

		case f0.IsDir():
			if 0 == level {
				logstr = fmt.Sprint(strings.Repeat("\t", level)+"Found Container Creating...", curdir)
				fmt.Println(logstr)
				if err = c.ContainerCreate(curdir, headers); err != nil {
					panicString := fmt.Sprint("PutContainer Error:", err)
					panic(panicString)
				}
			}
			worker(c, parent+"/"+f0.Name(), level+1)
		case !f0.IsDir():
			if strings.HasPrefix(f0.Name(), ".") {
				logstr = fmt.Sprint("Found Hidden Object. Ommited ", f0.Name(), " to ", curdir)
				fmt.Println(logstr)
				continue
			}

			contents, _err := ioutil.ReadFile(parent + "/" + f0.Name())
			if nil != _err {
				panicString := fmt.Sprint("PutObject Error:", _err)
				panic(panicString)
			}

			h := sha1.New()
			h.Write(contents)
			obj_sha1hash := hex.EncodeToString(h.Sum(nil))
			logstr = fmt.Sprint(strings.Repeat("\t", level)+"Found Object. Uploading ", f0.Name(), " [ ", obj_sha1hash, " ] to ", curdir)
			fmt.Println(logstr)

			fstream, err := c.ObjectCreate(curdir, f0.Name(), false, "", "application/zip", headers)
			if nil != err {
				panicString := fmt.Sprint("PutObject Error:", err)
				panic(panicString)
			}

			if counts, err := fstream.Write(contents); nil != err {
				panicString := fmt.Sprint("PutObject Error:", err)
				panic(panicString)
			} else {
				fmt.Printf("%vBytes Total: %v, Written: %v\n", strings.Repeat("\t", level), len(contents), counts)
			}

			fstream.Close()

			return ""
		}
	}
	return ""
}
コード例 #6
0
func CreatePublicContainer(name string, c swift.Connection) {
	headers := map[string]string{
		"X-Container-Read": ".r:*",
	}
	c.ContainerCreate(name, headers)
}