func ListDirectory()

in s3plugin/s3plugin.go [356:417]


func ListDirectory(c *cli.Context) error {
	var err error
	config, sess, err := readConfigAndStartSession(c)
	if err != nil {
		return err
	}
	bucket := config.Options.Bucket

	var listPath string
	if len(c.Args()) == 2 {
		listPath = c.Args().Get(1)
	} else {
		listPath = config.Options.Folder
	}

	client := s3.New(sess)
	params := &s3.ListObjectsV2Input{Bucket: &bucket, Prefix: &listPath}
	bucketObjectsList, _ := client.ListObjectsV2(params)
	fileSizes := make([][]string, 0)

	gplog.Verbose("Retrieving file information from directory %s in S3", listPath)
	for _, key := range bucketObjectsList.Contents {
		if strings.HasSuffix(*key.Key, "/") {
			// Got a directory
			continue
		}

		downloader := s3manager.NewDownloader(sess, func(u *s3manager.Downloader) {
			u.PartSize = config.Options.DownloadChunkSize
		})

		totalBytes, err := getFileSize(downloader.S3, bucket, *key.Key)
		if err != nil {
			return err
		}

		fileSizes = append(fileSizes, []string{*key.Key, fmt.Sprint(totalBytes)})
	}

	// Render the data as a table
	table := tablewriter.NewWriter(operating.System.Stdout)
	columns := []string{"NAME", "SIZE(bytes)"}
	table.SetHeader(columns)

	colors := make([]tablewriter.Colors, len(columns))
	for i := range colors {
		colors[i] = tablewriter.Colors{tablewriter.Bold}
	}

	table.SetHeaderColor(colors...)
	table.SetCenterSeparator(" ")
	table.SetColumnSeparator(" ")
	table.SetRowSeparator(" ")
	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
	table.SetHeaderLine(true)
	table.SetAutoFormatHeaders(false)
	table.SetBorders(tablewriter.Border{Left: true, Right: true, Bottom: false, Top: false})
	table.AppendBulk(fileSizes)
	table.Render()

	return err
}